• Resolved releone

    (@releone)


    Hello all,
    thanks to anyone that can help me to solve this loop.
    Basically I want to select posts that have:
    – a specific category
    AND
    – a specific tag
    – to be shown randomly
    – max 6 posts

    At the moment I’m using this code:
    <?php query_posts('cat=3&tag=test&orderby=rand&showposts=6'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    It doesn’t work as expected…it only shows results if I use “cat” or “tag” but not in combination…

    I’m not sure if query_post() is the best solution in order to filter posts that have a combination of “cat” AND “tag”.

    What do you suggest?

    Thanks in advance for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • efuller

    (@efuller)

    Please trying using WP_Query rather than query_posts().

    $args = array(
    	'cat' => 3,
    	'tag' => 'test',
    	'orderby' => 'rand',
    	'posts_per_page' => 6
    );
    
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
      // Do Stuff
    endwhile;
    endif;
    
    // Reset Post Data
    wp_reset_postdata();
    Thread Starter releone

    (@releone)

    hi efuller, thx a lot for the tip but I still have issues with my loop.

    I did different tests and basically seems that the system doesn’t accept a specific tag.

    At this point my question is: is there chance that could be a conflict of a specific tag in the wp code?

    Basically I have 3 different post_type (2 default “post” and “page” and 1 different “casino”), both “post” and “casino” have the tag “netent” but declared in different way:

    post_type = post
    tag = netent

    post_type = casino
    affiliate-tags = netent

    when I create the loop I use:

    <?php
    $args = array(
    'post_type' => 'casino',				'affiliate-tags' => 'netent',					'orderby' => 'rand',					'posts_per_page' => 6
    );
    $the_query = new WP_Query( $args );
    ?>
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    with the tag “netent” it doesn’t work and the objects in the loop are empty.

    Thread Starter releone

    (@releone)

    I teste also this code:

    $args = array(
                                    'post_type' => 'casino',
                                    'affiliate-tags' => 'featured',
                                    'orderby' => 'rand',
                                    'posts_per_page' => 6
                                );
                    $the_query = new WP_Query( $args );

    it is unbelivable that this loop works with an “affiliate_tags” => ‘featured’ but NOT with ‘affiliate_tags’ => ‘netent’ …. (empty result)

    helpppppppp

    Thread Starter releone

    (@releone)

    after many test I found the issue…the tag slug and tag name was not the same!!

    Btw thanks for your help, this thread could be closed, thx!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘query_post and combination of cat tag’ is closed to new replies.