This thread is over two years old and may be outdated. Please create a new thread if you need help, or email us if you have an active Premium license.

Widgets global variable

9 years ago · Last reply by Leza Rivolta 9 years ago

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.

Need fast email support? Get SiteOrigin Premium

Replies

2
  1. Alex S Staff 9 years, 14 days ago

    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. Leza Rivolta 9 years, 13 days ago

    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.

Have a different question or issue?

Start New Thread