[resolved] Skip Posts in Loop if has_tag (2 posts)

  1. Clicknathan
    Posted 4 years ago #

    I'm doing a query where I want to show 2 posts from a custom post type in my sidebar, but I'm also grabbing a variable from the URL string which equals the tag the post should be in, and then skip any posts in the loop that don't have that tag.

    Can anyone tell me what the code would be for [if (!has_tag('MYTAG')) skip this post]

    Here's my current code:

    <?php $bannerzip = $_GET["sk"]; ?>
    <?php $querystr = "SELECT * FROM (SELECT wposts.* FROM $wpdb->posts wposts WHERE wposts.post_status = 'publish' and wposts.post_type = 'cn_ad' ORDER BY rand()) as y limit 50 ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    if ($pageposts): foreach ($pageposts as $post):
    <?php if(has_tag($bannerzip) ) { ?><?php if ( has_post_thumbnail() ) { ?><?php the_post_thumbnail(); ?><?php } ?><?php } ?>
    <?php endforeach; endif; ?>

    Essentially, I'm just looking for the code that says "skip this post if it doesn't have a certain tag.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    <?php if( !has_tag('MYTAG') ) continue;?>

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.