1

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. Home page consists of 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 problem with the portfolio section. It is to be paged in order to use Infinite Scroll by Desandro - next posts are loaded after clicking "Load more" button. First 10 posts are these marked 'selected' (1st custom field) by the Client in 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 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..

  • 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 Mar 30 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 Mar 30 at 13:35
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') );

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.