wp_reset_query() wordpress – unable to reset the last query

I am trying to run two querys on a page.
The first shows the results of the custom user search
and the second is just supposed to display the more recent records with a few custom params hard coded into the query.

The problem i have is that when the custom search is run both queries are affected and the results are updated in both places.

Read More

I have tried placing these wp_reset_postdata();
wp_reset_query(); all over the place but all to no available.

Does anyone have any ideas?

This is my first query

$args = array('post_type' => 'vacancy', 'post_status' => 'publish' ) );
add_filter( 'posts_where', 'posts_where_title', 10, 2 );
function posts_where_title( $where, &$wp_query ) {
global $wpdb;
$where .= ' AND (' . $wpdb->posts . '.post_title LIKE '%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%'';
$where .= ' OR ' . $wpdb->posts . '.post_content LIKE '%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%')';
return $where;
}
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
//display results ?>
<?php $i++;  endwhile;  endif;  
wp_reset_postdata();
wp_reset_query();?>

This is my second query…

$args = array( 'post_type' => 'vacancy', 'posts_per_page' => 10 );
query_posts( "_vacancy_ends>=".date("Y-m-d")."&_vacancy_starts<=".date("Y-m-d")."&order=DESC" );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//display results
endwhile;
wp_reset_postdata();
wp_reset_query();

Related posts

Leave a Reply

1 comment

  1. Your problem is this line:

    query_posts( "_vacancy_ends>=".date("Y-m-d")."&_vacancy_starts<=".date("Y-m-d")."&order=DESC" );
    

    query_posts affects the main loop, and just about every other loop that runs in that given page request. There are very few circumstances when it would be considered good practice to use it over WP_Query or even get_posts as you’re affecting a LOT of the global variables WordPress uses to populate information about what is being viewed.

    Personally, I don’t typically use the query method on WP_Query, but perhaps it might help in this scenario. Try changing your second query to something like this:

    $args = array( 'post_type' => 'vacancy', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    $loop->query( "_vacancy_ends>=".date("Y-m-d")."&_vacancy_starts<=".date("Y-m-d")."&order=DESC" );
    while ( $loop->have_posts() ) : $loop->the_post();
        //display results
    endwhile;
    wp_reset_postdata();
    wp_reset_query();
    

    This localizes your query to a single instance of WP_Query, rather than affecting everything globally. It’s untested, but let me know if this helps.

    One more thing:

    add_filter( 'posts_where', 'posts_where_title', 10, 2 );
    function posts_where_title( $where, &$wp_query ) {
        global $wpdb;
        $where .= ' AND (' . $wpdb->posts . '.post_title LIKE '%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%'';
        $where .= ' OR ' . $wpdb->posts . '.post_content LIKE '%' . esc_sql( like_escape( $_POST['post_title'] ) ) . '%')';
        return $where;
    }
    

    Bear in mind that this filter will run on EVERY query you set after your call to add_filter. You might want to consider removing your filter at the point where more queries after it do not require the filter to run:

    remove_filter( 'posts_where', 'posts_where_title');