Support

Account

Home Forums General Issues Filtering a loop with taxonomy field

Solved

Filtering a loop with taxonomy field

  • I have a PAGE where i want to show articles that fit to this page. i use a taxonomy custom field to list the news-categorys. With this code i can show links to the assigned taxonomy:

    <?php 
        $terms = get_field('newskategorie');
        if( $terms ): ?>
        <ul>
        <?php foreach( $terms as $term ): ?>
        <?php echo $term->name; ?>
        <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    

    Now i wonder how it is possbible to show just lists to articles in the same taxonomy?

    E.g. i have the page “Tomatos” and i want to shows news (title + excerpt) from the category “red” and “vegetables” – not just links to the archive pages …

  • Hi @herrfischer

    Hmm… I believe you should be able to accomplish this using a tax_query in WP_Query. For example, the following code will query posts that are in the red and vegetable category:

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    	array(
    		'taxonomy' => 'category',
    		'field'    => 'slug',
    		'terms'    => array('red', 'vegetables')
    	),
    	),
    );
    $query = new WP_Query( $args );

    For more information, have a look at: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    Hope this helps 🙂

  • Thanks! Any idea how to do the same with a USER query??

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Filtering a loop with taxonomy field’ is closed to new replies.