I have two queries that I need to try to combine so that my pagination works correctly, and the posts show up in the correct order.
I have this one query:
$today = date('m/d/Y', strtotime('today'));
$args = array(
'post_type' => 'workshops',
"posts_per_page" => 5,
"paged" => $paged,
'meta_key' => 'select_dates_0_workshop_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'select_dates_0_workshop_date',
'meta-value' => "meta_value",
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
)
);
The results of this query need to come after the query above:
$args = array(
'post_type' => 'workshops',
"posts_per_page" => 5,
"paged" => $paged,
'meta_key' => 'select_dates_0_workshop_date',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'select_dates_0_workshop_date',
'meta-value' => "meta_value",
'value' => $today,
'compare' => '<',
'type' => 'CHAR'
)
)
);
The difference between the two queries is the: 'order'
and the 'compare'
.
I have done this in pure MYSQL Queries, but I am not sure how to do this on WordPress
This is an updated version of the answer, that’s more flexible then the previous one.
Here’s one idea using a SQL
UNION
:We can use the data from the
posts_clauses
filter to rewrite the SQL query from theposts_request
filter.We extend the
WP_Query
class to achieve our goal. We actually do that twice:WP_Query_Empty
: to get the generated SQL query of each sub-queries, but without doing the database query.WP_Query_Combine
: to fetch the posts.The following implementation supports combining
N
sub-queries.Here are two demos:
Demo #1:
Let’s assume you have six posts, ordered by date (DESC):
where the
XXX
,YYY
andZZZ
are older thanDT=2013-12-14 13:03:40
.Let’s order our posts so that posts published after
DT
are ordered by title (ASC) and posts publisehd beforeDT
are ordered by title (DESC):Then we can use the following:
This generates the following SQL query:
Demo #2:
Here’s your example:
This should give you a query like this one:
Demo #3:
We could also combine more than two sub queries:
Classes:
Here are our demo classes:
You can then adjust the classes to your needs.
I use the trick mentioned here to preserve the order of
UNION
sub queries.You can modify it accordingly with our
sublimit
parameter.This should also work for main queries, by using the
posts_request
filter, for example.I hope this helps.
The trouble lies in the ‘order’ argument: the ‘meta_query’ argument accepts an array of meta queries as well as the ‘relation’ argument (basically, “AND” or “OR”). This could be used to select posts both less-than and greater-than-or-equal-to
$today
(though at that point the meta query is useless as it’s selecting every (“OR”) or no (“AND”) workshops. Unfortunately, WP_Query doesn’t allow you to arbitrarily sort results (switching from closest upcoming to most recent past workshop).If you know how to pull this off directly in MySQL, you might consider looking at the WP_Query filters, specifically the
posts_orderby
filter. These filters enable you to start playing with the actual SQL being generated by the WP_Query class without having to switch to totally-custom queries. Be forewarned, however, that it’s important to limit the scope of these filters or risk them being applied to every query on your site (theposts_where
filter documentation has some good examples on how to do so).