Filtering navigation with pre_get_posts
-
I am trying to create a plugin to create a custom post type to track a user’s progress through a ten week program we offer. I have defined a custom post type (to simplify things here we’ll call it “week”) and created a post for each of the ten weeks. If a user is on week 3, I want to filter the week posts so that they are unable to view weeks beyond their current point.
I accomplished this by using “pre_get_posts” as listed below. This works as I would expect, however it does not seem to impact the calls to next_post_link. For example, when viewing week 3 during week 3, a link still appears for week 4 (although it is a dead link).
What is the proper way to impact a query so next_post_link does not emit a link on the last filtered post?
Code so far:
function xyz_pre_get_posts($query) { global $currentWeek; if ($query->is_main_query() && !is_admin() && is_single() && 'week' == $query->query_vars['post_type']) { $meta_query = array( array( 'key' => 'week', 'value' => $currentWeek, 'compare' => '<=' ) ); $query->set( 'meta_query', $meta_query ); } } add_action('pre_get_posts', 'xyz_pre_get_posts');
- The topic ‘Filtering navigation with pre_get_posts’ is closed to new replies.