I'm trying to build a function (inside a plugin), which limits the shown posts to the time a logged in user is registered in weeks. (So if a user is registered for 4 weeks the categories page shoud show only the 4 oldest posts). I tried to write something to calculate the registered weeks and then limit the post_per_page attribute. The site is using the Theme Blocksy, and the Theme Developer told us to use the parse_tax_query hook instead of pre_get_posts.

Here is my code (it's not working, the query is not limited and the frontend shows every published post)

Thank you in advance!

add_action( 'parse_tax_query', array( $this, 'filter_by_registration' ), 100, 3);

    function filter_by_registration( $query ) {
        $options = get_option('dh_options');
        if ($options['checkbox_usr_q']) {
            if ( !is_admin() && $query->is_main_query() && $query->in_category($cats)) {
            $posts_per_week = 1;
            if ($options['posts_per_week']) {
                $posts_per_week = $options['posts_per_week'];
            $cats = "54,55,56";
            $current_user = wp_get_current_user();
            $registrationdate = strtotime($current_user->user_registered);
            $timezone = date_default_timezone_get();
            $today = strtotime(date('m/d/Y h:i:s a', time()));
            $diff = $today - $registrationdate;
            $days_diff  = floor($diff/ 86400);
            $weeks_diff = floor($days_diff / 7);
            $post_per_page = $weeks_diff * $posts_per_week;
            $args = array (
                'posts_per_page' => $post_per_page,
                'order' => 'ASC',
                'orderby' => 'ID');
            $query->set( $args );
            return $query;
  • I don't understand why you would use parse_tax_query to do this, pre_get_posts combined with a date query parameter seems like a much better option. posts_per_page is not the solution, nor is the parse_tax_query filter
    – Tom J Nowell
    May 7 '21 at 12:00
  • @TomJNowell my idea was to make the post_per_page count fit to the amount registered weeks. E. g. if user1 is registered since 5 weeks he should see the posts for 5 oldest weeks, and if user2 is registered since 10 weeks he should be a able to see the posts for 10 weeks. Blocksy recommended to use the parse_tax_query hook insteadt of pre_get_posts, because the blocksy-theme archive page is using it.
    – bennyVen47
    May 7 '21 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.