I am trying to order posts using a few customs meta_key
-
Hi,
I am usually pretty good with SQL query and WordPress in general but I am still a total failure at using WordPress Query system.
I am trying to order posts on my website using a few custom parameters.
Here is the parameter, I am trying to use here :
is_featured (featured posts)
views (number of views)Both key, may or may not exists but if it doesn’t exists the post still needs to show up.
Here is the code :
add_action('pre_get_posts', 'filter_by_popularity'); if (!function_exists("filter_by_popularity")) { function filter_by_popularity($query) { if (is_home() && get_option("pq_filtre_pageaccueil")=="hotandhall") { if ($query->is_main_query()) { $query->set('meta_key', "is_featured"); $query->set('meta_key', "views"); // $query->set('meta_key', array("is_featured","views")); $query->set('orderby', 'is_featured DESC, views DESC, post_date DESC'); } } } }
When I only have one key it is working but I can’t seem to be able to filter two fields or more. I tried to use an array on “meta_key” but it doesn’t work either.
Thanks for your help.
Kindest regards,
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘I am trying to order posts using a few customs meta_key’ is closed to new replies.