Exclude old events from WP_Query with date saved in a `meta_key`

You need something like the following: $args = array( ‘post_type’ => ‘events’, ‘posts_per_page’ => 1, ‘meta_query’ => array( ‘meta_key’ => ‘event_start_date’, ‘type’ => ‘DATETIME’, // You can also try changing it to TIME or DATE if it doesn’t work ‘meta_value’ => date( “Y-m-d” ), ‘meta_compare’ => ‘>’, ), ‘order’ => ‘ASC’ );

How to change posts order on Category and Tag archives?

You can use pre_get_posts action to modify this order. Here’s the code: function my_change_posts_order( $query ){ if ( ! is_admin() && ( is_category() || is_tag() ) && $query->is_main_query() ) { $query->set( ‘order’, ‘ASC’ ); } }; add_action( ‘pre_get_posts’, ‘my_change_posts_order’); What we do here is: We’re adding our function that will modify params for WP_Queries We … Read more

How to display posts month by month?

WordPress 3.7 introduced the date_query to display posts month by month: $args = array( ‘date_query’ => array( array( ‘month’ => $month ) ) ); $query = new WP_Query( $args ); Note : $month refers to month number (1-12)

get_query_var returns null

You have to add any query vars that are not WordPress objects to the array of recognized query vars to be able to retrieve it from the $wp_query global: add_filter( ‘query_vars’, ‘wpa66452_query_vars’ ); function wpa66452_query_vars( $query_vars ){ $query_vars[] = ‘tree’; return $query_vars; }

Why is the first query affecting the second query, even after wp_reset_query() and wp_reset_postdata(), but not on the second page?

I see a number of issues, aside from the use of query_posts ( this should raise immediate alarm bells ). I strongly recommend you don’t use the alt syntax, and that you minimise the unnecessary opening and closing PHP tags as it obscures your code. Here is a cleaned up version of your first query … Read more

How would I get 1 latest post from a query for 5 posts?

You can use $current_post property of WP_Query $query_args = array( “posts_per_page” => “5” ); $listedPosts = new WP_Query($query_args); // the loop if ( $listedPosts->have_posts() ) { while ( $listedPosts->have_posts() ) { $listedPosts->the_post(); if ( (int) $listedPosts->current_post === 0 ) { // loop content for hero post } else { // loop content for remaining posts … Read more

How can I get an array of all IDs from the current queried object?

You are overwriting $post_ids variable on every while loop, never collecting them. That can be solved using $post_ids = array(); while (have_posts()) : the_post(); $post_ids[] = get_the_ID(); endwhile; var_dump($post_ids); // this is an array of ids However there is simpler way, you can skip the whle cycle and simply run: if( function_exists( ‘wpseo_local_show_map’ ) && … Read more

Use ‘parse_query’ filter to show posts that from multiple criteria

Ignore my comment about meta_query. Not only does it not work with $query->set(), you wouldn’t be able to control the critical “A OR B” requirement of the query. Instead, I believe what you require is possible via a combination of both the pre_get_posts action hook and posts_where filter hook as follows. add_action(‘pre_get_posts’, ‘my_show_appropriate_posts’); function my_show_appropriate_posts($query){ … Read more