I'm trying to filter archive_research.php to show only the research with status=current.
In functions.php:
add_action( 'pre_get_posts', 'only_current' );
function only_current( $query ) {
if ( $query->is_main_query() ) {
$args = array(
'post_type' => 'research',
'tax_query' => array(
array(
'taxonomy' => 'status',
'field' => 'slug',
'terms' => 'current',
),
),
);
$query = new WP_Query( $args );
}
}
In archive-research.php:
// theloop
if ( have_posts() ) : while ( have_posts() ) : the_post();
// print title etc
Result: research archive displays ALL research.
I have tried many slight alterations of this. For readability I will try to sum them up here without pasting a bunch of redundant code:
- removing the tax_query .. same result (expected)
- changing the tax_query to a taxonomy term that does not exist .. same result
- replacing
$query = new WP_Query( $args )
with$query->set('meta_query', $args)
or$query->set($args)
.. same result - replacing
have_posts()
andthe_post()
in archive-research to$wp_query->have_posts()
and$wp_query->the_post()
.. same result - adding a call to
do_action('only_current')
before// theloop
.. same result
So the common theme here is that no matter what I do, I get the same result. There must be a very obvious solution that I'm missing, and I would really appreciate it if someone could point it out.
Last bit of debugging info that might be useful:
string(222) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'research' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 10"
This is the result of calling echo var_dump($wp_query->request)
before // theloop
status
what your custom taxonomy` is called or do you have a custompost_status
?$query->set( 'tax_query', $taxquery )
- see wordpress.stackexchange.com/a/35263/57034