Got a custom field called startDate
but its only on a few events. I was wondering if it isn’t set for a post I could use post_date
to generate the posts list?
// if meta_key _postmeta.startDate isn't set get the rest by posts.post_date
query_posts(
array(
array(
'posts_per_page' => 10,
'meta_key' => 'startDate',
'meta_value' => date('Y-m-d'),
'meta_compare' => '<',
'orderby' => 'meta_value',
'order' => 'ASC'
),
array(
'meta_key' => 'post_date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '<'
)
)
);
If you can explain it in SQL, you can query for it! There are three places where we want to change the default query:
The join and the where-clause are added via the
_get_meta_sql()
function. The output is filtered, so we can hook into it:The order clause is filtered through
posts_orderby
:This gives us the following SQL query:
Remember to unhook the filters after you did your query, otherwise you will mess up other queries too. And if possible you should not call
query_posts()
yourself, but modify the main post query that is done by WordPress while setting up the page.In search of the same problem i came to this page. And was inspired by the answer from @jan Fabry, i added his solution.
Didnt work, due to filter_naming issues.
So i will post my updated version here, for other seekers:
and here the filters themselves:
try something along the lines of:
A query posts call makes only one query, not two. So no, you can’t have it make two separate queries and then concatenate the results.
Remember, you’re selecting some set of posts here, then displaying them. That set is selected all at once. If you want to get two separate sets of posts and then merge them, then that’s something you’ll have to do yourself with get_posts or similar.