Tagged: Blog, devin-docs, filter, grid, sorting
Is is possible to add a condition to the function below that would allow for filtering of one category chronologically and 1 alphabetically?
Dude/Peter helped me with this code in functions file to change default sorting to alphabetical however I need some categories output to be chronological….
add_filter(‘avia_post_slide_query’,’avia_order_by_random’, 10, 2);
function avia_order_by_random($query, $params)
{
$query[‘orderby’] = ‘title’;
$query[‘order’] = ‘ASC’;
return $query;
}
Thank you for your help!
Hey ttem!
You can try following code
add_filter('avia_post_slide_query','avia_order_by_random', 10, 2);
function avia_order_by_random($query, $params)
{
$query['orderby'] = 'title';
$query['order'] = 'ASC';
$terms = explode(',', $params['categories']);
if (!empty($terms) && in_array(55, $terms))
{
$query['orderby'] = 'date';
}
return $query;
}
and replace 55 with the id of the category you want to sort chronological.
Best regards,
Peter
Big Thanks Peter works great!
I added
$query['order'] = 'DESC';
so most recent posts show first.
Is it possible to specify multiple categories to sort by date:
if (!empty($terms) && in_array(55, $terms))
I tried adding another category id like below but that doesn’t seem to work
if (!empty($terms) && in_array(55,56, $terms))
Any idea how to aply the sorting to multiple categories in that function?
Thank you for your help!
Hey!
Please replace it with this:
add_filter('avia_post_slide_query','avia_order_by_random', 10, 2);
function avia_order_by_random($query, $params)
{
$query['orderby'] = 'title';
$query['order'] = 'ASC';
$terms = explode(',', $params['categories']);
if (!empty($terms) && in_array(55, $terms))
{
$query['orderby'] = 'date';
}
if (!empty($terms) && in_array(56, $terms))
{
$query['orderby'] = 'date';
}
return $query;
}
Best regards,
Ismael
Thank you again Ismael!