WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Posts for a specific Tag(s) on a Page? (9 posts)

  1. travelvice
    Member
    Posted 3 years ago #

    Howdy,

    I'm Googling my fingers off, but having a hard time finding a code snippet that will allow me to display a list of post titles on a Page for a specific tag.

    Purely for example purposes, lets say I have a Page of exotic fruits, and under the header for Peru I want to list all posts that have been tagged with 'fruit-peru'.

    How would I pass that request over to get_posts? I'm under the impression I first need to use get_the_tags.

    How would this change if I wanted to include more than one tag, say 'fruit-peru' and 'fruit-etc'?

    Thanks for the brain power!

  2. Bernhard Riedl
    Member
    Posted 3 years ago #

    Hi,

    First to clarify: As you have chosen tagpages as tag for your thread, it also landed in my plugin's support section. ;)

    Anyway, looking onto the source of get_posts you can see the following statement * @uses WP_Query::query() See for more default arguments and information. So you could use get_posts with tag-parameters of query_posts.

    Good luck,
    Berny

  3. travelvice
    Member
    Posted 3 years ago #

    Perhaps going right for WP_Query would be simpler?

    $query = new WP_Query( 'tag=fruit-peru' );

    ***

    // Create a new instance
    $second_query = new WP_Query( array( 'tag_slug__in' => array( 'fruit-peru', 'fruit-misc' ) ) );
    
    // The Loop
    while( $second_query->have_posts() ) : $second_query->the_post();
     echo '<li>';
     the_title();
     echo '</li>';
    endwhile;
    
    wp_reset_postdata();
  4. Bernhard Riedl
    Member
    Posted 3 years ago #

    I think it doesn't really make a difference if you use query_posts or create a new instance of WP_Query. I personally prefer the function, as this function is also documented in the API (template tags).

    Anyway, if you implement get_posts instead, you don't have to reset the 'original query'.

  5. travelvice
    Member
    Posted 3 years ago #

    I'm not sure how to make this query work w/ get_posts -- would you mind a snippet to explain?

  6. Bernhard Riedl
    Member
    Posted 3 years ago #

    Here you are...

    <?php
    $args = array('tag_slug__and' => array('fruit-peru', 'fruit-misc'));
    $postslist = get_posts( $args );
    foreach ($postslist as $post) :  setup_postdata($post); ?>
    	<ul>
    		<li><?php the_title(); ?></li>
    	</ul>
    <?php endforeach; ?>
  7. travelvice
    Member
    Posted 3 years ago #

    Neat, Berny!

    I am using your plugin for this test, so I suppose it should be noted that using get_posts isn't returning any results unless I specify 'post_type' => 'page' in the args array.

    This is also a problem with my snippet above. No results unless I specify pages.

    Not that I have a need at this very moment, but any idea how to merge these results (both posts and pages), should there be any posts that are also tagged as such?

    Cheers

  8. Bernhard Riedl
    Member
    Posted 3 years ago #

    The reason is that, post is the default post_type according to the documentation of get_posts.

    If you want to query for posts and pages use 'post_type' => array('post', 'page') as described in query_posts.

    Greetz,
    Berny

  9. travelvice
    Member
    Posted 3 years ago #

    Great! A huge help! Thank you, sir.

Topic Closed

This topic has been closed to new replies.

About this Topic