• I’ve inherited a site that I’m providing some support for and I want to exclude posts from a certain category in the blog feed. A custom plugin is being used in order to generate a default image when no featured image is set to the blog post and to set up the feed, which is generated when a short code is entered into a page.

    I’ve tried editing the ‘post_category’ => ”, section, both with slugs and with the category id to no avail.

    The website is https://english.shatil.org.il

    What else can I do? The main code for the plugin is below.

    <?php
    /*
    Plugin Name: Blog Posts Shortcode with Featured Image
    Plugin URI:
    Description: Custom Latest Blog Posts Shortcode with Featured Image Thumbnail. For use with Sterling Theme only.
    Author:
    Author URI:
    Version:1.0
    */
    
    function tt_bpfi_reqister_css(){
    wp_register_style('bp_shortcode_f_i_css',WP_PLUGIN_URL."/blog_posts_shortcode_featured_image/blog_posts_shortcode_featured_image.css",$deps,'1.0','screen');
    }
    add_action('wp_enqueue_scripts','tt_bpfi_reqister_css');
    
    /*-----------------------------------------------------*/
    /*	Recent Blog Posts with featured image
    /*-----------------------------------------------------*/
    function truethemes_blog_posts_f_i($atts, $content=null) {
    extract(shortcode_atts(array(
    'title'   => '',
    'count'   => '3',
    'character_count'   => '175',
    'post_category'   => '',
    ), $atts));
    
    $title = $title;
    $count = $count;
    $truethemes_count = 0; $truethemes_col = 0;
    
    global $post;
    $exclude = B_getExcludedCats();
    
    if ($post_category != ''){
    //mod by denzel to use WP_Query class instead of get_posts, so that WPML works.
    $myposts = new WP_Query('posts_per_page='.$count.'&offset=0&category_name='.$post_category.'');
    }else{
    $myposts = new WP_Query('posts_per_page='.$count.'&offset=0&category='.$exclude);
    }
    
    if ($title != '') {$output .= '[raw]<span class="section_title_two">'.$title.'</span>[/raw]';};
    
    if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post();
    
    		$permalink = get_permalink($post->ID);
    
    		//remove <!--nextpage--> and show only first page content
    		$post_content = explode('<!--nextpage-->',$post->post_content);
    		$post_content = (string)$post_content[0];
    		$post_content = substr(strip_tags($post_content),0,$character_count);
    		$post_content = rtrim($post_content); //remove space from end of string
    		$post_content = str_replace("","",$post_content);
    
        //remove all shortcodes from post content.
    		$post_content = strip_shortcodes($post_content);
    		$thumb = get_post_thumbnail_id();
    
    		//half width image details
    		$image_width = 65;
    		$image_height = 65;
    
    		if(empty($thumb)){
    		$image_url = WP_PLUGIN_URL."/blog_posts_shortcode_featured_image/images/blog_posts_featured_image.jpg";
    		$image_src = truethemes_crop_image($thumb,$image_url,$image_width,$image_height);
    		}else{
    		//assign half image src, uses function from truethemes_framework/global/basic.php
    		$image_src = truethemes_crop_image($thumb,$external_image_url,$image_width,$image_height);
    		}		
    
    $output .= '<div class="blog_posts_featured_holder">';
    $output .= '<div class="blog_posts_featured_image">';
    $post_thumb = "[raw]<a href='".$permalink."'><img src='".$image_src."' alt='".get_the_title()."' /></a>[/raw]";
    $output .= $post_thumb;
    $output .= '</div>';
    $output .= '<div class="blog_posts_featured_content">';
    $output .= '[raw]<strong><a href="'.$permalink.'">'.get_the_title().'</a></strong>[/raw]';
    $output .= '[raw]<p>'.$post_content.'...</p>[/raw]';
    $output .= '</div>';
    $output .= '<div class="hr hr-dotted">?</div>';
    $output .= '</div>';
    
    endwhile; endif;
    wp_reset_postdata();
    //link stylesheet in footer only when shortcode is used.
    wp_enqueue_style('bp_shortcode_f_i_css');
    
    return $output;
    }
    add_shortcode('blog_posts_featured_image', 'truethemes_blog_posts_f_i');
    ?>

    And addition to the child theme’s functions.php

    <?php 
    
    /* Insert your custom functions here */
    
    function trim_excerpt($text) {
      return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');
    
    // Featured Image script from https://wpforce.com/automatically-set-the-featured-image-in-wordpress //
    
    function wpforce_featured() {
              global $post;
              $already_has_thumb = has_post_thumbnail($post->ID);
                  if (!$already_has_thumb)  {
                  $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                              if ($attached_image) {
                                    foreach ($attached_image as $attachment_id => $attachment) {
                                    set_post_thumbnail($post->ID, $attachment_id);
                                    }
                               } else {
                                    set_post_thumbnail($post->ID, '4262');
                               }
                            }
          }  //end function
    add_action('the_post', 'wpforce_featured');
    add_action('save_post', 'wpforce_featured');
    add_action('draft_to_publish', 'wpforce_featured');
    add_action('new_to_publish', 'wpforce_featured');
    add_action('pending_to_publish', 'wpforce_featured');
    add_action('future_to_publish', 'wpforce_featured');
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Did you ever get a satisfactory answer to your problem here?

    I am wanting the same functionality. If you have a working solution, please let me know.

    Thanks!

    Hello;

    Just change this line;
    $myposts = new WP_Query(‘posts_per_page=’.$count.’&offset=0&category=’.$exclude);

    convert “category” to “cat” inside this line

    i mean:
    WP_Query(‘posts_per_page=’.$count.’&offset=0&cat=’.$exclude);

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need to exclude a category of posts from a blog feed’ is closed to new replies.