Awooga
Awooga

Reputation: 21

query_posts( $args) exclude a category

I am trying to exclude a category from a shortcode that shows latest posts. I have searched for a while for a solution to this but cant seem to get the different options to work.

This is my code, (unfortunately I didn't write it but need to edit it):

function blogcuts_homepage_sc($atts, $content = null) {
    $blogH = '';
    $last = '';
    $counter = 1;
    $args = array(
        'post_type' => 'post',
        'order' => 'DESC',
        'posts_per_page' => 4
    );
    query_posts( $args );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    if ($counter == $args['posts_per_page']) { $last = ' last-column'; }
        $img_array = wp_get_attachment_image_src(get_post_thumbnail_id(), 'small');
        $comments = get_comments_number();
        $custom = get_post_custom();

        $blogH.= '<article class="blog-excerpt'.$last.'">';
        $blogH.= '<h3><a class="bg-transition" href="'.get_permalink(get_the_ID()).'">'.get_the_title().'</a></h3>';
        $blogH.= '<a href="'.$img_array[0].'" rel="prettyPhoto" title="'.get_the_title().'">';
        $blogH.= get_the_post_thumbnail($post->ID, 'item', array('title'    => get_the_title()));
        $blogH.= '<div class="meta"><span class="date">'.get_the_date().'</span><div class="alignright"><span class="number-of-comments">'.$comments.'</span><a class="comments" href="'.get_permalink(get_the_ID()).'#comments"></a>';

        if (get_option('op_likeit') == 'Enabled') {
            $blogH.= '<span class="number-of-likes likes-'.get_the_ID().'">'.likes(get_the_ID()).'</span><a id="likeit-'.get_the_ID().'" class="liked-after-'.get_the_ID().' likes likeit'.liked(get_the_ID()).'" href="#"></a>';
        }
        $blogH.= '</div></div><p class="excerpt">'.$custom['desc'][0].'</p></article>';
        $counter++;
        endwhile; endif;
        wp_reset_query();
        return $blogH;
    }
}
add_shortcode("blogcuts_homepage", "blogcuts_homepage_sc");

The cat id is 23. I have tried adding 'exclude'=> '23', to the array but this does not work. Any suggestions or anyone able to point me in the right direction would be greatly appreciated!

Upvotes: 2

Views: 14614

Answers (1)

Lotus
Lotus

Reputation: 2656

Edit: get_posts() is the preferred way to do this, I think, as it won't interfere the_loop() (see below) Are you sure you want to use query_posts() and not get_posts()?

Query_posts alters the main loop, which might cause unexpected behavior when the user adds your shortcode to a page and then their page "breaks".

That being said, this code will use query_posts to exclude categories 1, 2, and 3:

Edit: removed "array_merge" and unecessary verbage 'post', 'order' => 'DESC', 'cat' => '-1,-2,-3', 'posts_per_page' => 4 ); query_posts( $args ); ?>

If you chose to use get_posts instead, it would probably look like this: 'post', 'order' => 'DESC', 'cat' => '-1,-2,-3', 'posts_per_page' => 4 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    ....  now you can operate just like in the_loop()
endforeach; 
wp_reset_postdata();
?>

http://codex.wordpress.org/Class_Reference/WP_Query

Exclude Posts Belonging to Category

Display all posts except those from a category by prefixing its id with a '-' (minus) sign.

$query = new WP_Query( 'cat=-12,-34,-56' );

Upvotes: 2

Related Questions