I have a custom ACF field set on all posts, called event_date
, I would like to grab all posts within a given category
, but have posts that have event_date
defined put first in DESC order of event_date
. The args
I'm using for query_posts
are as follows:
$args = array(
'posts_per_page' => -1,
'cat' => $category_id,
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date("Ymd", time()),
'compare' => $type == 'upcoming' ? '>=' : '<'
)
),
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_key' => 'event_date'
);
But the problem I'm facing is that the ACF field was created for post types, after all posts have been created, so many of these posts do not have event_date
even defined for the meta_key (which omits these posts from the query automatically when using query_posts($args)
. There are too many posts to go through all posts and save them all manually, would take forever.
Basically, if the meta_key
for any post does not have event_date
than I need to use the date
of the actual post itself (when it was published) to order by. I need all posts with event_date
shown first in DESC order of event_date
, than all posts that don't have event_date
defined, need to be shown after that using the published date of the post in DESC order. How can I do this? Preferably, all in 1 query_posts
call?
Thanks :)
query_posts
, it breaks the main query objects and a huge amount of other stuff that relies on the main query object$wpdb->get_results("SELECT STATEMENT, etc. etc.", ARRAY_A);
WP_Query
and query all posts normally (if this is not the main query, in which case you should be usingpre_get_posts
)pre_get_posts
will help here, as it might interfere with main wp query (tho not entirely sure of this). Can you please provide example?