Meta query with order by another custom field

Edit: I’ll try to elaborate on my problem. I am working on a portfolio site. Some custom post types are: Projects, Publications, Exhibitions, Lectures and Slides. The home page consists of the following sections:

  • slider with slide
  • Portfolio consist of:
  • Selected (customer-marked) projects, publications, exhibitions and lectures
  • and remaining projects, publications, exhibitions, and lectures
  • Rewards Section

Now, I have a problem with the portfolio section. This has to be paginated by Desandro using infinite scroll – the next posts are loaded after clicking the “load more” button. The first 10 posts are marked ‘selected’ (the first custom field) by the client in a specific order determined by the second custom field (from 1 to 10). Remaining posts are to be ordered by the descending date.

I used wp_query to query a custom post and a meta_query to bring these ‘selected’ posts to the top and get them in ascending order. I tried sorting the rest of the posts by date but I’m stuck.

Take a look at my wp_query args:

'post_type' => array( 'projekty', 'publikacje', 'wystawy', 'wyklady' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
    'relation' => 'OR',
    'home_rest' => array(
        'key' => 'sticky_home',
        'compare' => 'NOT EXISTS'
        ),
    'home_sticky' => array(
        'key' => 'sticky_home',
        'compare' => 'EXISTS'
        ),
'orderby' => array(
    'home_rest' => 'DESC',
    'home_sticky' => 'ASC',
    ),
'posts_per_page' => 10, 
'paged' => get_query_var('paged'), 

So I achieved the first goal (you can see the red blocks representing the ‘selected’ positions here: http://redesigned.pl/_sts/bxbstudio/strona-glowna-infinitemasonry/.

I thought I’d be able to do this multiple orderby sort first, sort sticky_home Fields (True/False fields) to get the selected positions at the top; Secondly, sort them by ascending sticky_home_order Farm; Third, sort the positions sticky_home Marked false Descending as of published date. Do you think it is possible to achieve all this in one query? Or maybe there is some other way to achieve this?

I would appreciate any help.

Edit: I’m no experienced PHP programmer. After reading various stuff on Stack Exchange, I think maybe using usort may Way. together with get_posts, But on the other hand, the notation will be broken..

Leave a Comment