0

I have created a custom taxonomy for my posts called "Regions." And I have tagged 2 posts with these regions. What I would like to do is list out any post that is tagged with a region, but for some reason it's not working. Here's my function to register the custom taxonomy:

function region_taxonomy() {

    $labels = array(
        'name' => _x( 'Regions', 'taxonomy general name' ),
        'singular_name' => _x( 'Region', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Regions' ),
        'all_items' => __( 'All Regions' ),
        'parent_item' => __( 'Parent Region' ),
        'parent_item_colon' => __( 'Parent Region:' ),
        'edit_item' => __( 'Edit Region' ), 
        'update_item' => __( 'Update Region' ),
        'add_new_item' => __( 'Add New Region' ),
        'new_item_name' => __( 'New Region Name' ),
        'menu_name' => __( 'Regions' )
    );

    register_taxonomy('regions',array('post'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'region' )
    ));

}

add_action('init', 'region_taxonomy', 0);

And here's the WP_Query from my page template:

<?php 
    $args = array( 
        'orderby' => 'date',
        'post_type' => 'post',
        'tax_query' => array(
            array(
                'taxonomy' => 'regions'
            ),
        ),
        'posts_per_page' => '-1'
    );
    $the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php the_content(); ?>
<?php endwhile; else: ?> 
    <p>Sorry, there are no posts to display</p> 
<?php endif; ?>
<?php wp_reset_query(); ?>

Unfortunately this is returning no posts even though I have 2 posts tagged with a Region. Any ideas?

2 Answers 2

2

Taxonomy query doesn't work without terms parameter. So you have to first query all the regions term then assign term ids to taxonomy query.

Here's the solution -

$terms = get_terms( array(
    'taxonomy' => 'regions',
    'fields'   => 'id=>slug',
) );

$args = array( 
    'orderby'        => 'date',
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'tax_query'      => array(
        array(
            'taxonomy' => 'regions',
            'field'    => 'term_id',
            'terms'    => array_keys( $terms ),
        ),
    ),
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        the_content();
    }
    wp_reset_postdata();
} else {
    echo '<p>Sorry, there are no posts to display</p>';
}

Btw, you shouldn't use wp_reset_query() it's costly and should be used with query_posts() only instead use wp_reset_postdata().

2
  • Great thank you, and thank you for the additional tip regarding resetting the query!
    – user13286
    Feb 27, 2018 at 20:37
  • 1
    Great to see that you already solved the problem :)
    – obiPlabon
    Feb 27, 2018 at 20:38
0

Apparently only specifying the taxonomy is not enough, I was able to solve my issue like this:

<?php
    $regions_terms = get_terms('regions');
    $regions = array();
    foreach ($regions_terms as $regions_term) {
        $regions[] = $regions_term->slug;
    }
    $args = array( 
        'orderby' => 'date',
        'tax_query' => array(
           array(
              'taxonomy' => 'regions',
              'field' => 'slug',
              'terms' => $regions
           )
        ),
        'posts_per_page' => '3'
    );
    $the_query = new WP_Query( $args );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.