• 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:

    <?php
    /*
    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 = '

    • ', $after_title = '
    • ', $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;
      }
      ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • http://codex.wordpress.org/Template_Tags/list_cats
    scroll down to “custom list with excluded categories”

    Thread Starter cgarvie

    (@cgarvie)

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

    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

    Thread Starter cgarvie

    (@cgarvie)

    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

    Thread Starter cgarvie

    (@cgarvie)

    any more ideas?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    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 ";

    Thread Starter cgarvie

    (@cgarvie)

    WORKS!

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

    Thanks again,
    Cameron

    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.

    thanks:)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Excluding categories from a random post plugin’ is closed to new replies.