Home Forums General Issues Query pages by Taxonomy Tag Name


Query pages by Taxonomy Tag Name

  • I’m sure this is super easy answer but I can’t get this to display what I am after.

    I have a custom taxonomy called “Services”

    I want to be able to display pages that come under the tag “oil”

    <?php $select_categories_to_show_terms = get_field( 'select_categories_to_show' ); ?>
    <?php if ( $select_categories_to_show_terms ): ?>
    	<?php foreach ( $select_categories_to_show_terms as $select_categories_to_show_term ): ?>
    	<?php $args = array(
        'post_type' => 'page',
        'tax_query' => array(
                'taxonomy' => 'services',
                'field'    => 'name',
                'terms'    => $select_categories_to_show_term->name,
    $query = new WP_Query( $args ); ?>
    	<?php endforeach; ?>
    <?php endif; ?>

    This displays nothing. I am hoping to get the 4 latest pages for this taxonomy tag

  • What is the value of $select_categories_to_show_term->name?
    This needs to be an array of term names. I don’t think it is because ACF does not have an option to return term names. You will need to loop over the array of terms that ACF returns and extract the names if you are returning term objects. If you are returning the term ID then it will get a little more complicated. On the other hand, if you are returning an array of term IDs then what you need to do is query by the term ID instead of the name.

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

The topic ‘Query pages by Taxonomy Tag Name’ is closed to new replies.