I looked at that and changing it to meta_value has no impact at all. The order_by = start_date comes from another query that I use on the homepage and that works!
It works here when ordering by meta value..
I’ve trimmed down the code used, but i needed something i could test with without recreating your whole setup..
So firstly, gave 4 or 5 posts a custom field called end date, and put in future dates in the format YYYY-MM-DD, then opened up the index and plonked this in before the loop..
if( 'events' == $wp_query->query_vars['pagename'] ) {
$args = array(
'meta_key' => 'end_date',
'meta_compare' => '>',
'meta_value' => date('Y-m-d'),
'orderby' => 'meta_value',
'order' => 'asc',
'posts_per_page' => 1,
'paged' => ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 )
);
query_posts($args);
}
Very basic, but it certainly grabs the correct posts (ordered by the end date), the conditional statement is only true on the events page, leaving the index to deal with other queries as it usually would.
This definitely works for ordering by the end date (which I would be happy with!). But it always displays the same data no matter what category you view, with was why we needed to merge with the original query.(I removed the if condition).
Feels like it’s getting closer though! Thanks for all your (continued…) help!
As said above, the conditional statement i placed around the code used makes sure the code only runs on the event page, so category posts just appear as they usually would, ie. not events posts.
If you can give me some suggestions on what i need to do to recreate your problem my end then perhaps i could suggest something further, but currently the simple test above suffered no such problems when viewing categories, etc..
I tested this code with the conditional statement and it worked fine. But I removed the conditional statement so that it would also work on the category pages and that was when it didn’t work.
As I understood it, with query_posts you could preserve the original query (e.g. cat=3) while you added other args. But that doesn’t seem to be happening.
Should the event code run for all cases? Or does it just need to merge on category views? (ie. when a category has been requested).
It should run in all cases. So whenever a category has been requested or a tag or a search.
This should work as far as i can test…
//global $wp_query; // Uncomment if necessary, shouldn't be
$args = array(
// ARGS HERE
);
$args = array_merge( $args , $wp_query->query );
query_posts( $args );
The $wp_query->query var should be an emtpy array or filled with query variables, so it shouldn’t fail the merge, either passing in your args, or your args + whatever exists in the query..
You’re a certifiable genius and my new hero 😉
Works like a charm. Thank you soo much.