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:
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:
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..
After fiddling and searching a bit I managed to have:
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')
);
sticky_home
as a taxonomy term, and have 2 termssticky_home
, andnot_sticky_home
, then this would see a major speed boost and be able to handle more visitors at the same time. Also, your use ofpaged
implies that this isn't a second query on the page, but a replacement query. Is there a reason you didn't usepre_get_posts
? – Tom J Nowell ♦ Commented Mar 30, 2020 at 12:55pre_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