Support » Fixing WordPress » Filtering a loop by a custom taxonomy term assignment

  • I have a custom taxonomy called ‘test’, and I want to filter the main loop to show only posts that are tagged with the term ‘test_term’.

    I tried using the ‘pre_get_posts’ and the ‘request’ filters, and it doesn’t seem to be working:

    //in functions.php
    add_action( 'pre_get_posts', 'test_pre_get_posts' );
    function test_pre_get_posts( $query ) {
    	$query->set('taxonomy','test');
    	$query->set('term','test_term');
    	return $query;
    }
    
    // and for the loop-index.php file in the theme:
    <?php  while ( have_posts() ) : the_post(); ?>
    ...
    <?php endwhile; ?>

    In query.php:1432 I printed out the value of $qv, and it does not seem to contain the changes I made in the pre_get_posts hook. The second conditional on line 1436 returns false as a result:

    isset($qv[$t->query_var])

    Thanks for the help,
    Dave Morris

Viewing 3 replies - 1 through 3 (of 3 total)
  • It seems that if I add this line:

    $wp_query->is_tax = true;

    And wrap it in an is_home() conditional statment, then it seems to work as I was expecting it to.

    Is this correct? Will it cause any other issues with other loops on the page?

    Thanks,
    Dave

    try

    query_posts(‘post_type=movie&NAMEOFTAXONOMY=CATEGORYNAMEOFTAXONOMY’);

    ex.:

    query_posts(‘post_type=movie&genre=drama’);

    That does work, but I want to hook this functionality in a plugin and have my theme handle the loop without any knowledge of the plugin. Does that make sense?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filtering a loop by a custom taxonomy term assignment’ is closed to new replies.