Meta query with order by another custom field

admin2025-01-07  4

Edit: I will try to explain in detail my problem. I am working on a portfolio site. There are a few custom post types: Projects, Publications, Exhibitions, Lectures, and Slides. The home page consists of the following sections:

  • slider with Slides
  • portfolio consisting of:
  • selected (marked by Client) Projects, Publications, Exhibitions, and Lectures
  • and remaining Projects, Publications, Exhibitions, and Lectures
  • awards section

Now, I have a problem with the portfolio section. It is to be paged in order to use Infinite Scroll by Desandro - the next posts are loaded after clicking the "Load more" button. The first 10 posts are these marked 'selected' (1st custom field) by the Client in a particular order determined by the 2nd custom field (from 1 to 10). The remaining posts are to be ordered by date descending.

I used wp_query to query custom posts and meta_query to get these 'selected' posts on top and sort them ascending. I tried to sort the remaining 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 red blocks representing 'selected' posts here: /.

I thought I would be able to do this multiple orderby sorting. Firstly, sort by sticky_home field (true/false field) to get selected posts on top; Secondly, sort them ascending by sticky_home_order field; Thirdly, sort posts with sticky_home marked false descending by date published. Do you think it is possible to get all of that in one query? or perhaps there is another way to achieve that?

I'll appreciate any help.

Edit: I am no experienced PHP programmer. Having read through various content on stack exchange, I think that perhaps using usort could be the way. together with get_posts? but on the other hand, pagination will brake..

Edit: I will try to explain in detail my problem. I am working on a portfolio site. There are a few custom post types: Projects, Publications, Exhibitions, Lectures, and Slides. The home page consists of the following sections:

  • slider with Slides
  • portfolio consisting of:
  • selected (marked by Client) Projects, Publications, Exhibitions, and Lectures
  • and remaining Projects, Publications, Exhibitions, and Lectures
  • awards section

Now, I have a problem with the portfolio section. It is to be paged in order to use Infinite Scroll by Desandro - the next posts are loaded after clicking the "Load more" button. The first 10 posts are these marked 'selected' (1st custom field) by the Client in a particular order determined by the 2nd custom field (from 1 to 10). The remaining posts are to be ordered by date descending.

I used wp_query to query custom posts and meta_query to get these 'selected' posts on top and sort them ascending. I tried to sort the remaining 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 red blocks representing 'selected' posts here: http://redesigned.pl/_sts/bxbstudio/strona-glowna-infinitemasonry/.

I thought I would be able to do this multiple orderby sorting. Firstly, sort by sticky_home field (true/false field) to get selected posts on top; Secondly, sort them ascending by sticky_home_order field; Thirdly, sort posts with sticky_home marked false descending by date published. Do you think it is possible to get all of that in one query? or perhaps there is another way to achieve that?

I'll appreciate any help.

Edit: I am no experienced PHP programmer. Having read through various content on stack exchange, I think that perhaps using usort could be the way. together with get_posts? but on the other hand, pagination will brake..

Share Improve this question edited Sep 28, 2022 at 16:07 seramo 133 bronze badges asked Mar 30, 2020 at 12:31 jakesh_of_redesignedjakesh_of_redesigned 193 bronze badges 2
  • this query will be slow/expensive, if you could store sticky_home as a taxonomy term, and have 2 terms sticky_home, and not_sticky_home, then this would see a major speed boost and be able to handle more visitors at the same time. Also, your use of paged implies that this isn't a second query on the page, but a replacement query. Is there a reason you didn't use pre_get_posts? – Tom J Nowell Commented Mar 30, 2020 at 12:55
  • @TomJNowell There will be 2 more queries on that page not related with that one, that is why I didn't use pre_get_posts. sticky_home as a taxonomy term? for each custom post that is queried? – jakesh_of_redesigned Commented Mar 30, 2020 at 13:35
Add a comment  | 

1 Answer 1

Reset to default 0

After fiddling and searching a bit I managed to have:

  • 'selected' 10 posts on top
  • 'remaining' posts below sorted descending by date

Unfortunately the 'selected' posts are in descending order ... here is my query:

$args = array(
    'post_type' => array( 'projekty', 'publikacje', 'wystawy', 'wyklady' ),
    'post_status' => array( 'publish' ),
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'sticky_home_order',
            'type' => 'numeric',
            'compare' => 'NOT EXISTS'
        ),
        array(
            'key' => 'sticky_home_order',
            'type' => 'numeric',
            'compare' => 'EXISTS'
        ),
    ),
    'orderby' => array(
        'meta_value' => 'DESC',
        'meta_value_num' => 'ASC', 
        'date' => 'DESC'
    ),
    'posts_per_page' => 20, 
    'paged' => get_query_var('paged')
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260992a706.html

最新回复(0)