1

I am using a customised archive template for a custom post type.

At the top of the page, I'd like to show a specific post, found using a new wp_query() call.

$args = [
    'posts_per_page' => 1,
    'post_type' => 'document',
    'order' => 'DESC',
    'orderby' => 'date',
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        the_title();
        the_date( 'F Y' );
    }
    wp_reset_postdata();
}

Custom search form

<form id="document-filter" method="post">
    <select name="order_by">
        <option value="date">Date</option>
        <option value="title">Name</option>
    </select>
    <select name="order">
        <option value="desc">DESC</option>
        <option value="asc">ASC</option>
    </select>
    <input name="s" type="text" placeholder="Filter by keyword" value=""/>
    <input name="post_type" type="hidden" value="document"/>
    <input type="submit" />
</form>

Initially, it works. However, if I then use the custom search form to either sort the archive list (date/name, asc/desc), the custom wp_query() is affected, and the query_vars array is overriding my supplied arguments.

Example

Posts, in date order, descending:

  1. Post C
  2. Post B
  3. Post A

Initially, my custom query at the top of the page will output Post C, which is correct.

If I use the search/filter form to change the order of the posts to:

  1. Post A
  2. Post B
  3. Post C

The singular queried post will now be Post A, which is incorrect. My custom query args have not changed, but are being overridden by the search form somehow.

  • I'm not sure what is the problem here. Can you add more information about the issue? – Drupalizeme Sep 26 '17 at 14:05
  • note that the_title and the_date output internally, those echo statements are unnecessary – Tom J Nowell Sep 26 '17 at 14:12
  • Also, how does the custom search form work? Looking at your query I don't see how anything on the template page could interfere with it, we'll need to see the custom search code – Tom J Nowell Sep 26 '17 at 14:13
  • @TomJNowell yea, I actually had some HTML mixed in but removed it for clarity. I've added the custom search form code in now. – Paul Canning Sep 26 '17 at 14:24
  • From the code so far I don't see why would global context possibly interfere with this standalone query. I would guess a case of something hooking into queries and not making proper checks to target just the right ones. – Rarst Sep 26 '17 at 14:42
0

In my experience wp_reset_postdata() does not work well. You can try this code:

global $wp_query;
$temp_wp_query = $wp_query;
//Your custom WP_Query code
$wp_query = $temp_wp_query;
  • Tried this solution, same issue. – Paul Canning Sep 27 '17 at 9:02
0

So I finally got this working, but not using wp_query().

Instead, I had to use a custom query with $wpdb.

Example:

$latest_document = $wpdb->get_results( $wpdb->prepare( "
        SELECT $wpdb->posts.*
        FROM $wpdb->posts
        WHERE $wpdb->posts.post_status = %s
        AND $wpdb->posts.post_type = %s
        ORDER BY $wpdb->posts.post_date DESC
        LIMIT 1
     ", 'publish', 'document' ), OBJECT );

What is interesting is that whilst I am only wanting a single row, when I used $wpdb->get_row() I got the same issue as before, where the resulting record would be sorted by the parameters from the search form.

Using $wpdb->get_results() works fine and the record is consistently the same, regardless of the sorting I select using the search form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.