Support » Developing with WordPress » Posts created under a TAG

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Howdy, howdy!

    Yes, it does make sense. What you are trying to do is is simply use a new instance of WP_Query. You could use the tag ID or the name making sure you get the right name. You could potentially use something like:

    
    $do_not_like = new WP_Query( array( 'tag' => 'green-eggs-and-ham' ) );
    
    // check if there are any posts to iterate over.
    if ( $do_not_like->have_posts() ) {
        // opening list tag.
        echo '<ul>';
        while ( $do_not_like->have_posts() ) {
            // this sets up all post data.
            $do_not_like->the_post();
    
            // so we can use functions like so :D
            echo '<li>' . get_the_title() . '</li>';
        }
        // close out the list tag.
        echo '</ul>';
    } else {
        // no posts found
    }
    
    // restore the post data.
    wp_reset_postdata();
    
    Moderator bcworkz

    (@bcworkz)

    Heya harshclimate,

    What you are describing sounds like what you get when you request something like example.com/tag/green-eggs-and-ham/, except you get more than just a title typically. Could it be you merely need to alter the output from such a request instead of starting a new query like Jose suggests?

    Jose’s code does deliver precisely what you asked for, but it’s not very flexible in regards to other tags you may want to query for. Where would the current tag of interest be coming from? An URL like in the previous paragraph? A tag assigned to the current post? You said you want to grab all tags. In what context? The current post? All tags that exist? Only tags actually associated with posts?

    With a little more information on what the lead up is to the results you want would allow us to give you a better answer.

    Thread Starter harshclimate

    (@harshclimate)

    Hi Jose and BC. Thanks for the responses!

    I’d like to grab all tags used on the website. So the way things work is, each post will have no more than two tags. On an “archives” page, I would like those tags used on each post to spit out on the archives page.

    Under each tag, then, have each post shown.

    Yeah, Jose’s would work, but it can’t be tag specific. It would have to burp out all the tags.

    Thread Starter harshclimate

    (@harshclimate)

    Okay, my last didn’t make sense…

    I only have two categories on the website. They are “Wineries” and “Breweries” in Arizona. Under those categories I would obviously have the names of those places. I would use the tags field to tag the city that these places are located.

    There!

    Moderator bcworkz

    (@bcworkz)

    The easiest solution would be to use get_terms() to get an array of all post_tag terms. Do a foreach loop through the terms array. Within this loop, place Jose’s code. Alter his code to replace the ‘green-eggs-and-ham’ tag argument with the current term name variable within the loop.

    You thus have nested loops, an outer term loop and an inner posts loop. This sort of setup isn’t very efficient. If there are a lot of tags, it takes a lot of post queries to complete the request. Despite that, it may likely work fine on your site, so there would be little reason to work up a more efficient solution.

    So far, the wineries and breweries are all together. Depending on how you want the list organized, you can differentiate by category as well by adding yet another nested loop and alter Jose’s WP_Query arguments to include the current category argument as well. If you only have a few categories, it may make for a nicer list primarily organized by location if you simply included a particular category icon (grapes or hops?) with each post listing. Then no category loop is required, instead use wp_get_post_categories() to determine what icon to output with the current post.

    Thread Starter harshclimate

    (@harshclimate)

    Well crap. I’m not good with this kinda stuff. I should probably come up with a different way of displaying the content. My coding skills are like… not having any 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Posts created under a TAG’ is closed to new replies.