• Resolved travelvice

    (@travelvice)


    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!

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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

    Thread Starter travelvice

    (@travelvice)

    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();

    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’.

    Thread Starter travelvice

    (@travelvice)

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

    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; ?>
    Thread Starter travelvice

    (@travelvice)

    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

    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

    Thread Starter travelvice

    (@travelvice)

    Great! A huge help! Thank you, sir.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display Posts for a specific Tag(s) on a Page?’ is closed to new replies.