Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: trying to get posts by tag

Tags:

wordpress

tags

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.

However, in a plugin that I'm writing using $wp_query no matter what parameters I pass in, I just get the complete list of posts back whether they have the tag that I am looking for or not.

Here's my code:

// Now retrieve all items matching this brand name . . .
$query=new WP_Query(array('posts_per_page=5', array('tag' => array($brand_name))));

// The Loop
while ( $query->have_posts() ) : $query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

This produces 10 results when I've told it only to return 5. In reality I should only get 2 posts back as that's the total number with the tag.

Looking around on the web there seems to be a lot of people having the same problem but no solutions. I must have tried about 10 different ways of specifying the tag but the fact that the number of posts returned is wrong suggests I've either got something completely wrong or there is some kind of bug. Wordpress version is 3.4.1 if it helps.

Can any Wordpress pro's shed light on this ?

Thanks in advance !

like image 726
user1647208 Avatar asked Sep 05 '12 00:09

user1647208


People also ask

How do I get post based on tags in WordPress?

Here's my code: // Now retrieve all items matching this brand name . . . $query=new WP_Query(array('posts_per_page=5', array('tag' => array($brand_name)))); // The Loop while ( $query->have_posts() ) : $query->the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; // Reset Post Data wp_reset_postdata();

How do tags work in WordPress?

Tags provide a useful way to group related posts together and to quickly tell readers what a post is about. Tags also make it easier for people to find your content. Tags are similar to categories, but they are generally used to describe your post in more detail. The use of tags is completely optional.

Can you add tags to pages in WordPress?

Adding Categories and Tags to WordPress Pages. The easiest way of setting up categories and tags for your WordPress pages is by using the Pages with category and tag plugin. It's a free WordPress plugin and works out of the box with no need for additional configuration.


2 Answers

Answer was found here - https://codex.wordpress.org/Template_Tags/get_posts

Following example displays posts tagged with 'jazz', under 'genre' custom taxonomy, using 'tax_query'

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'genre',
            'field' => 'slug',
            'terms' => 'jazz'
        )
    )
);
$postslist = get_posts( $args );

So for you it will be

$args = array( 
        'posts_per_page' => 5,
        'tax_query'      => array(
            array(
                'taxonomy'  => 'post_tag',
                'field'     => 'slug',
                'terms'     => sanitize_title( $brand_name )
            )
        )
    );

$postslist = get_posts( $args );
like image 98
BeRocket Avatar answered Oct 10 '22 14:10

BeRocket


Try this

$original_query = $wp_query;
$wp_query = null;
$args = array('posts_per_page' => 5, 'tag' => $brand_name);
$wp_query = new WP_Query($args);

if (have_posts()) :
    while (have_posts()) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
endif;

$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
like image 30
The Alpha Avatar answered Oct 10 '22 14:10

The Alpha