Home>Support>Widgets global variable

Widgets global variable

Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

Hello

I have placed a layout builder widget, inside this layout builder widget i have some self created recent posts widgets.

– layout builder widget
– recent posts (1)
– recent posts (2)
– recent posts (3)

I need to store the post_id value for every widget in global array. So i can exclude already displayed posts in the next widget. This approach was working fine before a few releases of site origin.

This was the code:

$recentPosts = wp_get_recent_posts([
	'numberposts' => 1,
	'offset' => 0,
	'category' => 0,
	'orderby' => 'post_date',
	'order' => 'DESC',
	'include' => '',
	'exclude' => $instance['exclude'],
	'meta_key' => '',
	'meta_value' =>'',
	'post_type' => 'post',
	'post_status' => 'draft, publish, future, pending, private',
	'suppress_filters' => true
], OBJECT);

if(is_numeric($recentPosts[0]->ID))
{
	array_push($GLOBALS['recentpostWidgetExclude'], $recentPosts[0]->ID);
}

In the latest release, this is not working anymore. I have tried to find other solutions with function modify_instance or with hook.

add_filter('siteorigin_widgets_instance_recentpost-widget', 'wbe_filter_button_frontend_instance', 10, 2);

Any ideas.
Greetings
Leza

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 8 years, 5 months ago Alex S
    Hi, I Work Here

    Hi Leza,

    Can you please try and remember when your code stopped working? This will allow me to try and find why this may have stopped working at the time.

    Can you please send me the complete code you’re trying to use?

  2. 8 years, 5 months ago Leza Rivolta

    Hello Alex S

    Thanks for your answer. I was working on this project before season holidays. Then i stopped working and resumed working at the end of january. Before season holidays the code was working.

    That is what i have done.
    I added a variable to my functions.php to use as global in widget template file.

    $recentpostWidgetExclude = [];

    Then in my widget template file, i use wp_get_recent_posts with exclude $GLOBALS[‘recentpostWidgetExclude’]. If a post was found, i store the post_id in $GLOBALS[‘recentpostWidgetExclude’] to exclude in next widget.

    $recentPosts = wp_get_recent_posts([
    	'numberposts' => 1,
    	'offset' => 0,
    	'category' => 0,
    	'orderby' => 'post_date',
    	'order' => 'DESC',
    	'include' => '',
    	'exclude' => $GLOBALS['recentpostWidgetExclude'],
    	'meta_key' => '',
    	'meta_value' =>'',
    	'post_type' => 'post',
    	'post_status' => 'draft, publish, future, pending, private',
    	'suppress_filters' => true
    ], OBJECT);
    
    if(is_numeric($recentPosts[0]->ID))
    {
    	array_push($GLOBALS['recentpostWidgetExclude'], $recentPosts[0]->ID);
    }
    
Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More