How to hide a post from the main page and category using a tag
-
Hi there,
I am having trouble with trying to hide posts from the main index page and category page using certain tags.
For instance, if I add a tag to a post that says “exclude”, that’s a label to not show the post anywhere on the main page or category.
I don’t really know what to add since I’m not a programmer, but I know that I need to find the ID number of this tag. How can I go on from here?
-
Check out this example.
https://wp-snippets.com/exclude-posts-with-certain-tag-from-loop/
That didn’t seem to work, it forced my whole site to go down.
Is there a way I can exclude posts preferably by an ID number?
Use this
tag__not_in=4
so it may looks something like
query_posts( 'tag__not_in=4' );
Well I’ll be honest, I don’t know what I’m really doing since I’m not a programmer. I keep making the site go down. I’ll paste the code here to see if anyone can give me a lend.
<?php get_header(); ?> <div id="load_posts_container"> <?php $category_ID = get_category_id('blog'); $args = array( 'post_type' => 'post', 'posts_per_page' => 12, 'cat' => '-' . $category_ID, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1) ); query_posts($args); $x = 0; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; if($paged > 1) $y = (0 + (($paged-1) * 12)); else $y = 0; while (have_posts()) : the_post(); ?> <?php if($x == 2) { ?> <div class="home_post_box home_post_box_last" onmouseover="show_post_desc(<?php echo $y; ?>)" onmouseout="hide_post_desc(<?php echo $y; ?>)"> <?php } else { ?> <div class="home_post_box" onmouseover="show_post_desc(<?php echo $y; ?>)" onmouseout="hide_post_desc(<?php echo $y; ?>)"> <?php } ?> <!--<img src="<?php bloginfo('stylesheet_directory'); ?>/images/blog-image.jpg" />--> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-post',array('alt' => 'post image', 'class' => 'rounded')); ?></a> <div class="home_post_desc" id="home_post_desc<?php echo $y; ?>"> <?php $temp_arr_content = explode(" ",substr(strip_tags(get_the_content()),0,225)); $temp_arr_content[count($temp_arr_content)-1] = ""; $display_arr_content = implode(" ",$temp_arr_content); echo $display_arr_content . '...'; ?> </div><!--//home_post_desc--> <div class="home_post_title_cont"> <h3><?php the_title(); ?></h3> <h4><?php the_category(', '); ?></h4> </div><!--//home_post_title_cont--> </div><!--//home_post_box--> <?php if($x == 2) { $x = -1; /*echo '<div class="clear"></div>';*/ } ?> <?php $x++; $y++; ?> <?php endwhile; ?> <?php wp_reset_query(); ?> <div class="clear"></div> </div><!--//load_posts_container--> <div class="load_more_cont"> <p align="center"><span class="load_more_text"><?php next_posts_link('<img src="' . get_bloginfo('stylesheet_directory') . '/images/load-more-image.png" />') ?></span></p> </div><!--//load_more_cont--> <script type="text/javascript"> // Ajax-fetching "Load more posts" $('.load_more_cont a').live('click', function(e) { e.preventDefault(); //$(this).addClass('loading').text('Loading...'); //$('.load_more_text a').html('Loading...'); $.ajax({ type: "GET", url: $(this).attr('href') + '#main_container', dataType: "html", success: function(out) { result = $(out).find('#load_posts_container .home_post_box'); nextlink = $(out).find('.load_more_cont a').attr('href'); //alert(nextlink); //$('#boxes').append(result).masonry('appended', result); $('#load_posts_container').append(result); //$('.fetch a').removeClass('loading').text('Load more posts'); //$('.load_more_text a').html('Load More'); if (nextlink != undefined) { $('.load_more_cont a').attr('href', nextlink); } else { $('.load_more_cont').remove(); $('#load_posts_container').append('<div class="clear"></div>'); // $('.load_more_cont').css('visibilty','hidden'); } if (nextlink != undefined) { $.get(nextlink, function(data) { //alert(nextlink); if($(data + ":contains('home_post_box')") != '') { //alert('not found'); // $('.load_more_cont').remove(); $('#load_posts_container').append('<div class="clear"></div>'); } }); } } }); }); </script> <?php get_footer(); ?>
In the $args = array(, add
‘posts_per_page’ => 3,
3 is the tag id.
Let me know how it goes.
Hi Ryan, that only makes the total post to 3. But I’ve figured it out!
I added this:
‘tag__not_in’ => array(‘6’); //6 is the tag ID for any WP newbie like myself
<?php $category_ID = get_category_id('blog'); $args = array( 'post_type' => 'post', //I added this simple code! 'tag__not_in' => array('6'), 'posts_per_page' => 12, 'cat' => '-' . $category_ID, 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1) );
Now I have trouble excluding this tag in the category section!Any clue on that?
Here’s my category php
<?php get_header(); ?> <div id="load_posts_container"> <?php $x = 0; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; if($paged > 1) $y = (0 + (($paged-1) * 12)); else $y = 0; global $wp_query; $args = array_merge( $wp_query->query, array( 'posts_per_page' => 9 ) ); query_posts( $args ); while (have_posts()) : the_post(); ?> <?php if($x == 2) { ?> <div class="home_post_box home_post_box_last" onmouseover="show_post_desc(<?php echo $y; ?>)" onmouseout="hide_post_desc(<?php echo $y; ?>)"> <?php } else { ?> <div class="home_post_box" onmouseover="show_post_desc(<?php echo $y; ?>)" onmouseout="hide_post_desc(<?php echo $y; ?>)"> <?php } ?> <!--<img src="<?php bloginfo('stylesheet_directory'); ?>/images/blog-image.jpg" />--> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('home-post',array('alt' => 'post image', 'class' => 'rounded')); ?></a> <div class="home_post_desc" id="home_post_desc<?php echo $y; ?>"> <?php $temp_arr_content = explode(" ",substr(strip_tags(get_the_content()),0,225)); $temp_arr_content[count($temp_arr_content)-1] = ""; $display_arr_content = implode(" ",$temp_arr_content); echo $display_arr_content . '...'; ?> </div><!--//home_post_desc--> <div class="home_post_title_cont"> <h3><?php the_title(); ?></h3> <h4><?php the_category(', '); ?></h4> </div><!--//home_post_title_cont--> </div><!--//home_post_box--> <?php if($x == 2) { $x = -1; /*echo '<div class="clear"></div>';*/ } ?> <?php $x++; $y++; ?> <?php endwhile; ?> <?php wp_reset_query(); ?> <div class="clear"></div> </div><!--//load_posts_container--> <div class="load_more_cont"> <p align="center"><span class="load_more_text"><?php next_posts_link('<img src="' . get_bloginfo('stylesheet_directory') . '/images/load-more-image.png" />') ?></span></p> </div><!--//load_more_cont--> <script type="text/javascript"> // Ajax-fetching "Load more posts" $('.load_more_cont a').live('click', function(e) { e.preventDefault(); //$(this).addClass('loading').text('Loading...'); //$('.load_more_text a').html('Loading...'); $.ajax({ type: "GET", url: $(this).attr('href') + '#main_container', dataType: "html", success: function(out) { result = $(out).find('#load_posts_container .home_post_box'); nextlink = $(out).find('.load_more_cont a').attr('href'); //alert(nextlink); //$('#boxes').append(result).masonry('appended', result); $('#load_posts_container').append(result); //$('.fetch a').removeClass('loading').text('Load more posts'); //$('.load_more_text a').html('Load More'); if (nextlink != undefined) { $('.load_more_cont a').attr('href', nextlink); } else { $('.load_more_cont').remove(); $('#load_posts_container').append('<div class="clear"></div>'); // $('.load_more_cont').css('visibilty','hidden'); } if (nextlink != undefined) { $.get(nextlink, function(data) { //alert(nextlink); if($(data + ":contains('home_post_box')") != '') { //alert('not found'); // $('.load_more_cont').remove(); $('#load_posts_container').append('<div class="clear"></div>'); } }); } } }); }); </script> <?php get_footer(); ?>
Haha sorry, my bad lol
For the category page, try to replace the code
$args = array_merge( $wp_query->query, array( 'posts_per_page' => 9 ) );
to
$args = array_merge( $wp_query->query, array( 'posts_per_page' => 9, 'tag__not_in' => 6 ) );
Great!! Thanks a lot for your help Ryan, you’re awesome!
No worries ??
- The topic ‘How to hide a post from the main page and category using a tag’ is closed to new replies.