WordPress.org

Ready to get started?Download WordPress

Forums

Filtering a loop by a custom taxonomy term assignment (4 posts)

  1. dvmorris
    Member
    Posted 4 years ago #

    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

  2. dvmorris
    Member
    Posted 4 years ago #

    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

  3. Rafael Ehlers
    Member
    Posted 4 years ago #

    try

    query_posts('post_type=movie&NAMEOFTAXONOMY=CATEGORYNAMEOFTAXONOMY');

    ex.:

    query_posts('post_type=movie&genre=drama');

  4. dvmorris
    Member
    Posted 4 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags