Get tag posts wordpress

I’ve written some code which automatically creates some posts and adds a tag to them. I can see the tags in the ‘All posts’ admin panel and I can click on the posts ‘Tag’ link to get just those posts with the tags.

Here is my code:

Read More
<?php $tag_ID= single_tag_title();
                    $args = array(
                        'post_type' => 'post',
                        'tag_id'    =>  $tag_ID,
                        'posts_per_page' => 10,
                        'order'          =>'ASC'
                    );
                    $posts = get_posts( $args );
                    var_dump($args );
                    foreach ( $posts as $post ) {
                        ?>

Can you help me to get all tag posts?
Thank you.

Related posts

1 comment

  1. Create a new file (tag.php) in wp-content/themes/yourthemefolder/
    and put below code in it.

    <?php
    get_header();
    
    $tag = single_tag_title('', false);
    echo '<h1>Tag: ' . $tag . '</h1>';
    
    $args = array(
        'post_type' => 'post',
        'taxonomy' => $tag,
        'terms' => $tag,
        'posts_per_page' => 10,
        'order' => 'ASC'
    );
    $postslist = get_posts($args);
    
    foreach ($postslist as $post) :
        setup_postdata($post);
        ?>
        <div id="post">
            <h2>Post title:<?php the_title(); ?></h2>
    
            <p><?php the_content(); ?></p>
        </div>
    <?php endforeach; 
    get_footer(); ?>
    

Comments are closed.