2

I need to fetch all the posts with a specific tag, but I'm getting all the posts instead. My query works if I publish a post with the tag that I need and list all the posts with that tag, but when I publish a post with another tag, it's fetching the newly published post.

This is my query:

$original_query = $wp_query;
$wp_query = null;
$args=array(
    'posts_per_page' => -1, 
    'tag' => $post_tag
);
$wp_query = new WP_Query( $args );
$post_titles=array();
$i=0;
if ( have_posts() ) :
    while (have_posts()) : the_post();
        $post_titles[$i]=get_the_ID() ;
        $i++;
    endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
  • How are you getting $post_tag. And your query is the same as query_posts which should not be used at all. – Pieter Goosen Oct 15 '14 at 10:13
  • i have specific tag i used statically – Antwan Oct 15 '14 at 10:19
7

It's a lot easier to create a new WP_Query than it is to try clearing or overwriting the original.

If $post_tag is a tag slug, you could simply use:

<?php
$the_query = new WP_Query( 'tag='.$post_tag );

if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
  • yes $post_tag is the tag slag but i didn't understand whats the mean difference in your code sorry i m newbie to wordpress – Antwan Oct 15 '14 at 9:58
  • The main difference is that your code attempts to nullify the main WP Query instead of just creating a new one. – Courtney Ivey Oct 15 '14 at 10:05
  • i m getting no post found i m sorry but is there any dif between tag and tag slug? – Antwan Oct 15 '14 at 10:09
  • In the Query, you can use slugs or ids. – Courtney Ivey Oct 15 '14 at 10:12
  • it's works now but i added new tag in english and it's works but i have tags in arabic so it's dont work on them – Antwan Oct 15 '14 at 10:18
1

In your functions.php

/* Display Related Products */
/* ======================== */

if ( ! function_exists( 'display_related_products' ) ) {

    function display_related_products($post_tag) {
        ?>
        <div class="related-products">

            <!-- simple WP_Query -->
            <?php
                $args = array(
                    'post_type' => 'product',
                    'tag' => $post_tag, // Here is where is being filtered by the tag you want
                    'orderby' => 'id',
                    'order' => 'ASC'
                );

                $related_products = new WP_Query( $args );
            ?>

            <?php while ( $related_products -> have_posts() ) : $related_products -> the_post(); ?>

                <a href="<?php the_permalink(); ?>" class="related-product">
                    <?php if( has_post_thumbnail() ) : ?>
                        <?php the_post_thumbnail( 'full', array( 'class' => 'related-product-img', 'alt' => get_the_title() ) ); ?>
                    <?php endif; ?>
                </a>

            <?php endwhile; wp_reset_query(); ?>

        </div>
        <?php
    }
}

Call from anywhere with

display_related_products('name-of-the-tag');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.