Home » Wordpress » exclude category from wordpress post

exclude category from wordpress post

Posted by: admin November 30, 2017 Leave a comment

Questions:

I want to exclude category from shoowing my blog posts. My category id is 62. category name is perfect_work

Here is my wordpress blog template code:

    <div id="left" class="eleven columns">

    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('paged='.$paged);
    ?>

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

        <div class="post" id="post-<?php the_ID(); ?>">

            <div class="title">

                <h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?></a></h2>

                <div class="postmeta">  <span>by <?php the_author_posts_link(); ?></span> | <span><?php the_time('l, F jS, Y') ?></span> | <span><?php the_category(', '); ?></span> </div>

            </div>

            <div class="entry">

            <?php $image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'top_feature'); ?>    

                <a href="<?php the_permalink() ?>"><img src="<?php echo $image_attr[0]; ?>" class="postim scale-with-grid" id="blog-thumb" ></a>
                <?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
                <div class="clear"></div>
            </div>
        </div>

    <?php endwhile; ?>
    <?php getpagenavi(); ?>
    <?php $wp_query = null; $wp_query = $temp;?>
</div>

I already tried using

$wp_query = new WP_Query('cat=-62');

its not work. I also put

<?php query_posts('cat=-62'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

Its work but page navigation not work, and also not showing others post. only 1st 5 post show.

Any Solution?

Answers:

Get the page number

$paged = get_query_var('paged') ? get_query_var('paged') : 1;

Then you may use

$wp_query = new WP_Query('cat=-62&paged=' . $paged);

Or use

$cat_id = get_cat_ID('perfect_work');
$wp_query = new WP_Query('cat=-' . $cat_id . '&paged=' . $paged);

Then loop

if($wp_query->have_posts()) :
    while ($wp_query->have_posts()) : $wp_query->the_post();
        // ...
    endwhile;
endif;

Questions:
Answers:

Try this one you have to specify the showposts to limit the posts

<?php $wp_query->set( 'cat', '-62' ); ?>
<?php query_posts( 'showposts=10' ); ?> 
<?php if( have_posts() ) : ?> 
<?php while( have_posts() ) : the_post(); ?>

.
.
.
<?php endwhile; ?>
<?php endif; ?>

Note : The minus sign indicates the exclusion of all Posts which
belong to that category from being retrieved from the database. In
turn, the Loop will never have Posts of that category id and only
process the specified number of Posts of other category ids.

Questions:
Answers:

Please read the codex on WP_Query, it is imo very detailed, look at the category params part

Just add a minus sign - in front of the categories you dont want, so the below code would mean show posts with category 10 and 11, but exclude category 62

$recent = new WP_Query("showposts=3&cat=10,11,-62")

Questions:
Answers:

You don’t need to use the $temp variable before or after the query. You should use something like this:

//This should do the trick
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$args = array(
    'cat' => -62,
    'paged' => $paged
);

// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

  <!-- the loop -->
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
  <?php endwhile; ?>
  <!-- end of the loop -->

  <!-- pagination here -->
  //The real trick!
  <?php wp_reset_postdata(); ?>

Two things to note:

  • The paged query parameter
  • To reset the query use wp_reset_postdata()