The best way to store variable across different widgets

The front page of my site building with set of widgets. Each widget shows the rows with posts for some criteria.

I want to exclude duplicate posts across different widgets. The simplest way comes to mind is to use global variable.

For example:

global $exclude;

$query = new WP_Query([
  'posts_per_page' = publish, 
  'post__not_in' = $exclude
]);

$exclude = wp_list_pluck( $query-posts, 'ID' );

I want to find better way to do this job. I've thought about wp_get_cache, but it seems that it is not what I want.

Topic post-duplication wp-query Wordpress

Category Web


Unfortunately no one answers my question so I will show my way to solve this problem.

I use public query vars with get_query_var and set_query_var functions to set widget__exclude variable across all queries.

Still not sure that it is the best way for this case but hope it helps somebody.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.