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']);
...
}
}
I want to exclude the assigned post_id
of $post
from my loop:
if (have_posts()) : while (have_posts()) : the_post();
1. How to get the
post_id
value?WordPress stores widget data in the options table with
option_name
iswidget_{$id_base}
. Example, when you construct a widget like this:The
option_name
should bewidget_so37244516-widget
. Then to retrieve the widget data, we just need to use: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.Remember to do
wp_reset_query()
.functions.php
:Make sure to change
is_home()
to your preference page.You need to use pre get posts hook.
Tyr this code
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.!
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
Detailed example
Referrence link:Click me