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:
- Post C
- Post B
- 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:
- Post A
- Post B
- 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.
the_titleandthe_dateoutput internally, those echo statements are unnecessary