• I have a custom widget plugin that lists post links in the sidebar. I want it to exclude the post if it is already on that page.
    My code is below – I’ve commented out the lines where I have tried to achieve excluding the main post (as they don’t work). Its working fine without these lines.

    // Front End Display
    function widget($args, $instance) {
    echo $args[‘before_widget’];
    if ( ! empty( $instance[‘title’] ) ) {
    echo $args[‘before_title’] . apply_filters( ‘widget_title’, $instance[‘title’] ) . $args[‘after_title’];
    }
    
    $args = array(
    // global $post;
    // $exclude = $GLOBALS[‘current_id’];
    //$args = array(
    ‘post_type’ => ‘events’,
    //’post__not_in’ => array($exclude),
    // );
    ‘posts_per_page’	=> 5,
    ‘orderby’	=> ‘date’,
    ‘order’	=> ‘ASC’,
    ‘tax_query’ => array(
    ‘relation’ => ‘AND’,
    //Term ‘Exhibitions’
    array(
    ‘taxonomy’ => ‘types’,
    ‘field’ => ‘id’,
    ‘terms’ => array( 13 ),
    ‘operator’ => ‘IN’
    ),
    //Not with term ‘Archive’
    array(
    ‘taxonomy’ => ‘status’,
    ‘field’ => ‘id’,
    ‘terms’ => array( 7 ),
    ‘operator’ => ‘NOT IN’
    )
    )
    
    );
    • This topic was modified 7 years, 9 months ago by bdbrown. Reason: code block
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The “post__not_in” argument in the arguments array is the correct approach, but getting the proper IDs to suppress because they are on the page is the tricky part because widgets are outside the Loop. On single post pages, you can get the ID from get_queried_object_id().

    On list type pages you need to collect all the post IDs on the page. The main query should still be in the global $wp_query if all subsequent queries properly reset themselves. The posts on the page are in $wp_query->posts. You can use array_column() to extract all the IDs for use in the “post__not_in” argument.

    Thread Starter powerj

    (@powerj)

    Awesome. The second part above is beyond me – if I understand correctly this is relative to archive pages.

    But the first part worked a treat and I think excluding them on single pages only will be fine. Many thanks

    I tried many different existing featured posts widget plugins (which do the excluding out of the box) but couldn’t get them to include one taxonomy AND exclude a term from another taxonomy.

    THANKS AGAIN FOR YOUR HELP

    Moderator bcworkz

    (@bcworkz)

    Right, archives or any other sort of non-single posts listing. My suggestion requires all other code that makes post queries to properly reset their query, so it’s not 100% reliable. The worst that will happen is the wrong posts will be excluded. This is the gist of it:

    global $wp_query;
    $ids = array_column( $wp_query->posts, 'ID');

    Then in your query arguments array, include 'post__not_in'=> $ids,

    Come to think of it, this technique should work for single posts as well, a one size fits all solution!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sidebar Widget – Exclude main post from list’ is closed to new replies.