WordPress.org

Forums

Exclude category from popular post list. (2 posts)

  1. ydwer
    Member
    Posted 5 years ago #

    I trie to exclude a category from my popular post list without a plugin.

    Till now I have the following code. I searched internet for solutions but couldn't find the right one. As I am not a the best in php maybe someone can help me how to expand the function so it will be possible to exclude some categories.

    Thanks in advance.

    function dp_popular_posts($num, $pre='<li>', $suf='</li>', $excerpt=false) {
    	global $wpdb;
    	$querystr = "SELECT $wpdb->posts.post_title, $wpdb->posts.ID, $wpdb->posts.post_content FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ORDER BY $wpdb->posts.comment_count DESC LIMIT $num";
    	$myposts = $wpdb->get_results($querystr, OBJECT);
    	foreach($myposts as $post) {
    		echo $pre;
    		?><a class="title" href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title ?></a><?php
    		if ($excerpt) {
    			dp_attachment_image($post->ID, 'medium', 'alt="'.$post->post_title.'"');
    			?><p><?php echo dp_clean($post->post_content, 85); ?>... <a href="<?php echo get_permalink($post->ID); ?>">Read More</a></p><?php
    		}
    		echo $suf;
    	}
    }
  2. MichaelH
    Member
    Posted 5 years ago #

    Didn't test this but something like:

    function dp_popular_posts($num, $pre='<li>', $suf='</li>', $excerpt=false) {
      global $wpdb;
      $querystr = "SELECT $wpdb->posts.post_title, $wpdb->posts.ID, $wpdb->posts.post_content FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ORDER BY $wpdb->posts.comment_count DESC LIMIT $num";
      $myposts = $wpdb->get_results($querystr, OBJECT);
      foreach($myposts as $post) {
        setup_postdata($post);
        if (!in_category(7) {
          echo $pre;
          ?><a class="title" href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title ?></a><?php
          if ($excerpt) {
            dp_attachment_image($post->ID, 'medium', 'alt="'.$post->post_title.'"');
            ?><p><?php echo dp_clean($post->post_content, 85); ?>... <a href="<?php echo get_permalink($post->ID); ?>">Read More</a></p><?php
          }
          echo $suf;
        } // if(!in_category
      }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic