lundi 18 mars 2019

Using WP Query order by custom field and then random

I am trying to use WP Query to get the information from the database. I'm trying to order the post by menu_order and if the menu order is the same standard 0 then the posts should be randomised.

 SELECT   sus_wp_posts.* FROM sus_wp_posts  LEFT JOIN sus_wp_term_relationships ON (sus_wp_posts.ID = sus_wp_term_relationships.object_id) WHERE 1=1  AND (sus_wp_term_relationships.term_taxonomy_id IN (16,17,18,19,20)) AND sus_wp_posts.post_type = 'portfolio-item' AND ((sus_wp_posts.post_status = 'publish')) GROUP BY sus_wp_posts.ID ORDER BY menu_order ASC, RAND()

This does the trick but I can't get the orderby params like i this with WP_Query. I tried it like this:

 $query_array = array(
        'post_status'    => 'publish',
        'post_type'      => 'portfolio-item',
        'posts_per_page' => $params['number_of_items'],
        'orderby'        => array('menu_order', 'post_title'),
        'order'          => 'ASC'
    );

But then I get the following output:

SELECT   sus_wp_posts.* FROM sus_wp_posts  LEFT JOIN sus_wp_term_relationships ON (sus_wp_posts.ID = sus_wp_term_relationships.object_id) WHERE 1=1  AND ( 
  sus_wp_term_relationships.term_taxonomy_id IN (16,17,18,19,20)
) AND sus_wp_posts.post_type = 'portfolio-item' AND ((sus_wp_posts.post_status = 'publish')) GROUP BY sus_wp_posts.ID




Aucun commentaire:

Enregistrer un commentaire