Excluding categories from a random post plugin (9 posts)

  1. cgarvie
    Posted 9 years ago #

    Hey, what I want to do is basically do what this following plugin does, but exclude my NEWS category - and in the future possibly more categories.

    Here is the plugin, thanks in advance:

    Plugin Name: Random Posts
    Plugin URI: http://www.w-a-s-a-b-i.com/archives/2004/05/27/wordpress-random-posts-plugin/
    Description: Displays a configurable list of random posts. Usage: random_posts();
    Version: 1.1
    Author: Alexander Malov
    Author URI: http://www.w-a-s-a-b-i.com/

    function random_posts ($limit, $len, $before_title = '

  2. ', $after_title = '
  3. ', $before_post = '', $after_post = '', $show_pass_post = false, $show_excerpt = false) {
    global $wpdb, $tableposts;
    $sql = "SELECT ID, post_title, post_content FROM $tableposts WHERE post_status = 'publish' ";
    if(!$show_pass_post) $sql .= "AND post_password ='' ";
    $sql .= "ORDER BY RAND() LIMIT $limit";
    $posts = $wpdb->get_results($sql);
    $output = '';
    foreach ($posts as $post) {
    $post_title = stripslashes($post->post_title);
    $post_title = str_replace('"', '', $post_title);
    $permalink = get_permalink($post->ID);
    $post_content = strip_tags($post->post_content);
    $post_content = stripslashes($post_content);
    $output .= $before_title . '' . $post_title . '' . $after_title;
    if($show_excerpt) {
    $words=split(" ",$post_content);
    $post_strip = join(" ",array_slice($words,0,$len));
    $output .= $before_post . $post_strip . $after_post;

    echo $output;

  • Samuel B
    Posted 9 years ago #

    scroll down to "custom list with excluded categories"

  • cgarvie
    Posted 9 years ago #

    thank you, but I want random posts to appear from all but one category, not categories to appear except for one.

  • katesgasis
    Posted 9 years ago #

    try using this sql

    $cat_to_exclude = <category id you want excluded>;

    SELECT p.ID, p.post_title, p.post_content FROM $wpdb->posts p, $wpdb->post2cat pc WHERE post_status = 'publish' AND $wpdb->post2cat.post_id=ID AND $wpdb->post2cat.category_id <> $cat_to_exclude

  • cgarvie
    Posted 9 years ago #

    thanks for your help - im now getting this:

    WordPress database error: [Unknown table 'wp_post2cat' in where clause]
    SELECT p.ID, p.post_title, p.post_content FROM wp_posts p, wp_post2cat pc WHERE post_status = 'publish' AND wp_post2cat.post_id=ID AND wp_post2cat.category_id <> 4 AND post_password ='' ORDER BY RAND() LIMIT 5

    Warning: Invalid argument supplied for foreach() in /home/g1c9comc/public_html/tutorialdose/wp-content/plugins/random-posts.php on line 21

  • cgarvie
    Posted 9 years ago #

    any more ideas?

  • Try this query in there instead.

    $sql = "SELECT p.ID, p.post_title, p.post_content FROM $wpdb->posts p, $wpdb->post2cat pc WHERE p.post_status = 'publish' AND pc.post_id=p.ID AND pc.category_id <> $cat_to_exclude ";

  • cgarvie
    Posted 9 years ago #


    wow, thanks a lot! I know SQL but not to such an extent. You guys are miracle workers.

    Thanks again,

  • vlp
    Posted 8 years ago #

    hi, thanks for posting the way how to exclude category - any help how to exclude all categories except one? I tried several possibilities but still get just errors.


  • Topic Closed

    This topic has been closed to new replies.

    About this Topic