It seems the plugin does not exclude posts via pre_get_posts
-
Hi there,
On a magazine I’m developing I’ve decided to use YARPP and it seems to be a great plugin.
Since some pages consists in many loops and related posts are rendered via PHP at different levels of correlation, I managed to exclude already displayed posts in subsequent loops by using a global variable, hooked in the_post, like:
global $shown_posts; function post_is_shown($post) { global $shown_posts; $shown_posts[] = $post->ID; } add_action('the_post', 'post_is_shown', 100);Then I’m excluding the collected post ids on subsequent Wp_Query-ies by using pre_get_posts hook, this way:
function exclude_shown($query) { if(is_admin()) return; $exclude_ids = get_shown_posts(); if(!empty($exclude_ids)) { $query->set('post__not_in', $exclude_ids); } } add_action('pre_get_posts', 'exclude_shown', 100);(the get_shown_posts() above just gets the variable and yes, I’ll later refactori this in a class in order to avoid globals…).
With its simplicity, this method works pretty well with any plugin/theme loops I’m using, as they just have to use any WordPress’s function leading to a WP_Query in order to exclude ids.
However, I’ve noticed that YARPP still displays already shown posts.
I’ve checked the code in YARPP_Core class and I’ve noticed that it makes use of WP_Query, however I’m not fully understanding how at the moment, because I see you’re passing $reference_ID as ‘p’ parameter, which should result in that specific post returned for the query…
I was wondering if there’s a way to avoid related repetition when multiple related-posts boxes are displayed.
Thanks!
The topic ‘It seems the plugin does not exclude posts via pre_get_posts’ is closed to new replies.
