Support » Fixing WordPress » excluding more than one category from random post

  • Resolved ejm

    (@llizard)


    I found a couple of random post plugins for the sidebar but they all call for using widgets and come with their own styling that seems like too much work to customize.

    I decided to use the coding recommended in the codex: http://codex.wordpress.org/Template_Tags/get_posts#Random_posts

    But I had a little difficulty figuring out how to exclude more than one category. After googling, I came across the following page
    http://blog.websitestyle.com/index.php/2006/11/02/wordpress-how-to-exclude-categories-from-a-feed/ to finally come up with this:

    $args = array( 'posts_per_page' => 5, 'orderby' => 'rand', 'category' => -4, 'category' => -7, 'category' => -13 );

    It works, but there must be a shorter way to exclude more than one category! Please advise.

    Thank you.

    E Morris

    This is the full coding I have right now:

    <!-- start random post links -->
    <li><strong><?php _e('5 randomly chosen posts:'); ?></strong></li>
    <?php
    $args = array( 'posts_per_page' => 5, 'orderby' => 'rand', 'category' => -4, 'category' => -7, 'category' => -13 );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    	<li><a href="<?php the_permalink(); ?>"  title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    <!-- end random post links -->

Viewing 2 replies - 1 through 2 (of 2 total)
  • Can you use something like this:
    $args = array( 'posts_per_page' => 5, 'orderby' => 'rand', 'category__not_in' => array(4, 7, 13) );

    Thread Starter ejm

    (@llizard)

    Oops! So sorry, I completely forgot to check back. Yes, that works perfectly well. Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘excluding more than one category from random post’ is closed to new replies.