The best way to store variable across different widgets
-
09-03-2021 - |
Question
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.
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange