WordPress: Custom loop to exclude post id assigned within widget $args

Having widget registered in function.php to display defined post_id meta:

class featured_widget extends WP_Widget
{
  /**
     * Display front-end contents.
     */
    function widget($args, $instance)
    {
        $post = get_post($instance['post_id']);
...
}

}

Read More

I want to exclude the assigned post_id of $post from my loop:

if (have_posts()) : while (have_posts()) : the_post();

Related posts

4 comments

  1. 1. How to get the post_id value?

    WordPress stores widget data in the options table with option_name is widget_{$id_base}. Example, when you construct a widget like this:

    function __construct() {
        parent::__construct('so37244516-widget',
            __('A label', 'text-domain'), [
            'classname'   => 'so37244516-widget-class',
            'description' => __('Some descriptions', 'text-domain')
        ]);
    }
    

    The option_name should be widget_so37244516-widget. Then to retrieve the widget data, we just need to use:

    $data = get_option('widget_so37244516-widget');
    

    But, because a widget can have multiple instances, $data is an associative array with unpredictable keys. (Each time we drag a widget into a sidebar and save it, a new instance of the widget is returned).

    So if there’s only one instance of the widget throughout your site, $data[2]['post_id'] is the value we need. And if there’re multiple instances, we need to loop through $data, compare some keys and values to find out the correct one. As always, var_dump($data) is very helpful.

    2. Exclude the post of post_id from the loop.

    Suppose $exclude_id is the value we got from step 1.

    1. You’re doing a custom loop, use @hemnath_mouli’s method:
    $query = new WP_Query([
        'post__not_in' => [$exclude_id]
    ]);
    
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post();
            // Do loop.
        endwhile;
        wp_reset_query(); // Must have.
    else :
        // Do something.
    endif;
    

    Remember to do wp_reset_query().

    1. You’re using default loop, try @Deepti_chipdey’s method in your functions.php:
    add_action('pre_get_posts', function($query)
    {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set('post__not_in', [$exclude_id]);
        }
    });
    

    Make sure to change is_home() to your preference page.

  2. You need to use pre get posts hook.

    Tyr this code

    function exclude_single_posts_home($query) {
      if ($query->is_home() && $query->is_main_query()) {
        $query->set('post__not_in', array($post));
       }
    }
    
    add_action('pre_get_posts', 'exclude_single_posts_home');
    
  3. If you want to exclide a post, then you must use post__not_in in WP_Query

    $post = new WP_Query( array( 'post__not_in' => array( $exclude_ids ) ) );

    Hope this would help you.!

  4. If you want to Exclude a single post follow the steps which mentioned above

    But unless you give post id by separately just made all post which you want to to exclude in a category and exclude it by simple way.

    Exclude Posts From Some Category

        <?php $query = new WP_Query( 'cat=-3,-8' ); ?>// 3 and 8 are category id
    

    Detailed example

        <?php $query = new WP_Query( 'cat=-3,-8' ); ?>
        <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
        <div class="post">
    
        <!-- Display the Title as a link to the Post's permalink. -->
         <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
        <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
       <small><?php the_time( 'F jS, Y' ); ?> by <?php the_author_posts_link(); ?></small>
    
        <div class="entry">
         <?php the_content(); ?>
        </div>
    
         <p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
         </div> <!-- closes the first div box -->
    
         <?php endwhile; 
              wp_reset_postdata();
              else : ?>
              <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
          <?php endif; ?>
    

    Referrence link:Click me

Comments are closed.