Display posts where tag matches page title

I’m trying to create a loop that displays a list of posts with a tag that matches the page title that the loop is on.

For example, I have a list of custom post types called ‘countries’ and within each country I have a recent posts list. For each country I want to display posts with a tag relevant to that country. So if a post contains the tag ‘UK’ then only these posts should be displayed on the ‘UK’ page.

Read More

Here’s my code so far which does not work at all…

    $country_tag = get_the_title(); 

    global $wp_query;
    $args = array(
    'tag__in' => 'post_tag', //must use tag id for this field
    'posts_per_page' => -1); //get all posts

    $posts = get_posts($args);
    foreach ($posts as $post) :
    //do stuff 
    if ( $posts = $country_tag ) {
    the_title();
    }
    endforeach;

Related posts

1 comment

  1. Assuming that you’ve got the correct value in $country_tag, and assuming that (per your question) $country_tag is the tag name (and not the tag slug or ID), then you must either use the Taxonomy Parameters in your get_posts, or first get the ID or slug of the tag. You can do this using get_term_by

    Additionally, before you can operate on the post, you need to call setup_postdata.

    I recommend using the get_term_by first, so you can first check if the tag exists and output a message if it does not.

    $country_tag = get_the_title(); 
    
    $tag = get_term_by( 'name', $country_tag, 'post_tag' );
    
    if ( ! $country_tag || ! $tag ) {
        echo '<div class="error">Tag ' . $country_tag . ' could not be found!</div>';
    } else {
        // This is not necessary.  Remove it...
        // global $wp_query;
        $args = array(
            'tag__in'        => (int)$tag->term_id,
            'posts_per_page' => -1
        );
    
        $posts = get_posts( $args );
        // be consistent - either use curly braces OR : and endif
        foreach( $posts as $post ) {
            // You can't use `the_title`, etc. until you do this...
            setup_postdata( $post );
            // This if statement is completely unnecessary, and is incorrect - it's an assignment, not a conditional check
            // if ( $posts = $country_tag ) {
                the_title();
            // }
        }
    }
    

    Above I am recommending the get_term_by method, because it allows you to first validate that there is a tag with that name. If you were 100% confident that there is always a tag that corresponds to the page title, you could use the taxonomy parameters (demonstrated below):

    $country_tag = get_the_title(); 
    
    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'field'    => 'name',
                'terms'    => $country_tag
            )
        ),
        'posts_per_page' => -1
    );
    
    $posts = get_posts( $args );
    foreach( $posts as $post ) {
        setup_postdata( $post );
        the_title();
    }
    

Comments are closed.