Skip Navigation

[Resolved] Hide some content on the post archives page

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 25 replies, has 1 voice.

Last updated by amanT 1 week, 4 days ago.

Assisted by: Minesh.

Author
Posts
#2809846

For this :
"On this page: hidden link

First it shows four pages.

Then I appy the filter "Book categories" and selected the option "Action and Adventure" it filters the results correctly and shows 2 pages as results.

Is this correct? if yes:

When I click on page 2 link it redirect me on 2nd page and it does not show 4 pages."
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Initially, the page displays 4 pages. However, if you click directly on page 2 without applying any filters, it shows 5 pages.

Now, regarding the filter functionality, specifically the "Filter by Points" option:
If you select 4000 points, a book appears in the results. When you view this book, you'll see a red message stating:
"This book cannot be added to the library as the author does not currently have enough points."
This indicates that the book should actually be hidden, but it is incorrectly shown when filters are applied.

If you go into the book archive editor and change the Custom Search Settings from
"AJAX refresh when changing any filter" to "Full page refresh when clicking Submit",
then apply the same 4000 points filter and click the Search button, you'll see the message "No items found."
This confirms that the book is correctly hidden in this mode.

However, I do not want to use the Search button or trigger a full page reload. I want the filters to auto-refresh via AJAX when changed, but still correctly hide the book that shouldn't be displayed

#2809854

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure but when I checked it fired multiple ajax requests when I click on the pagination page 2. I mean if you click on any pagination link either page 2,3 or 4 etc.. it fires multiple ajax requests.

I suggest you should go with non-ajax solution that works and without any issue.

#2809855

The pagination part is fine, I'll stick with the non-AJAX, manual approach, no problem. But regarding the custom search (filter) and also 'sort by' functionality: right now, it only works properly if I add and click that 'Search' button each time. I'd prefer it to automatically filter as soon as a filter option is selected, without needing to press an extra button. What's the best way to implement that?

#2809856

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I jsut checked and when you filter the results both search and pagination works.

Regarding sorting - you just have to change the direction and it does work but without ajax.

I'm not sure why the sorting is not working with ajax as it should. If you want I can offer you a sandbox site where you can try and paly with sandbox or if you want I can setup a sandbox with sorting feature that should work with ajax.

If that works with sandbox that means there must be some conflict with theme/plugin or anything else you use with your current site. Please let me knnow what way you would prefer.

#2809857

Can I share a working video where I've highlighted the issues so that you understand better?

#2809910

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

First of all, I've set the sorting control to filter the result without reloading the page. Please check the following screenshot that shows the setting:
=> hidden link

Then, I've adjusted the code added to your code snippet "Hide Test" as given under:
=> hidden link

add_action('pre_get_posts', 'hide_reviewed_books_for_user',10,1);
function hide_reviewed_books_for_user($query) {

	global $WP_Views;

	
	/// or (wp_doing_ajax() and (isset($_REQUEST['view_number']) and $_REQUEST['view_number']==14508))
	
    if ( ($query->is_main_query() && !is_admin() && $query->is_post_type_archive('book') && is_user_logged_in() )
		   ) {
		
	
        
        $current_user_id = get_current_user_id();

        $args = array(
            'post_type' => 'review',
            'author' => $current_user_id,
            'fields' => 'ids',
            'posts_per_page' => -1,
        );

        $user_reviews = get_posts($args);
        $reviewed_books = array();

        if (!empty($user_reviews)) {
            foreach ($user_reviews as $review_id) {
                $book_id = toolset_get_related_post($review_id, 'book-review');
                if ($book_id) {
                    $reviewed_books[] = $book_id;
                }
            }
        }

		
        $args = array(
            'post_type' => 'book',
            'posts_per_page' => -1,
            'fields' => 'ids',
        );

        $books = get_posts($args);
        $books_to_hide = array();

        foreach ($books as $book_id) {
            $book_author_id = get_post_field('post_author', $book_id);
            $author_points = mycred_get_users_balance($book_author_id);
            $points_to_deduct = get_post_meta($book_id, 'wpcf-points-to-deduct', true);
            $points_to_deduct = intval($points_to_deduct);

            if ($author_points < $points_to_deduct) {
                $books_to_hide[] = $book_id;
            }
        }

        $hidden_books = array_merge($reviewed_books, $books_to_hide);
		
		
		

        if (!empty($hidden_books)) {
            $query->set('post__not_in', $hidden_books);
        }
    } else if( wp_doing_ajax()){
		
		global $wpdb;
		
$current_user_id = get_current_user_id();

$user_reviews = $wpdb->get_col(
    $wpdb->prepare(
        "
        SELECT ID 
        FROM {$wpdb->prefix}posts 
        WHERE post_type = %s 
          AND post_status = %s 
          AND post_author = %d
        ",
        'review',
        'publish',
        $current_user_id
    )
);
		
		     if (!empty($user_reviews)) {
            foreach ($user_reviews as $review_id) {
                $book_id = toolset_get_related_post($review_id, 'book-review');
                if ($book_id) {
                    $reviewed_books[] = $book_id;
                }
            }
        }
		
	
$book_results = $wpdb->get_col(
    $wpdb->prepare(
        "
        SELECT ID 
        FROM {$wpdb->prefix}posts 
        WHERE post_type = %s 
          AND post_status = %s 
         
        ",
        'book',
        'publish'
    )
);
		
	
		

     $reviewed_books = array();
		$books_to_hide = array();
		
		foreach ($book_results as $book_id) {
            $book_author_id = get_post_field('post_author', $book_id);
            $author_points = mycred_get_users_balance($book_author_id);
            $points_to_deduct = get_post_meta($book_id, 'wpcf-points-to-deduct', true);
            $points_to_deduct = intval($points_to_deduct);

            if ($author_points < $points_to_deduct) {
                $books_to_hide[] = $book_id;
            }
        }


        $hidden_books = array_merge($reviewed_books, $books_to_hide);
		
		
		// $not_in = array(24531,24508,24506,24504,24501);
		$query->set('post__not_in', $hidden_books);
		
	}
			
}

Can you please confrim it works as expected.

#2810026

Can you recheck the code?
It seems like pagination, filter options, and sorting aren't working now. They just keep loading without displaying any results

#2810061

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please try to delete the browser cache and see if that help you to resolve your issue.

At this end, its working as expected.

#2810283
Screenshot 2025-05-26 112842.png

No, same. Please check the screenshot

#2810288

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

May I know what browser you are using?

However - I tried my best in order to offer you the solution with the custom code.

If you require further help with such custom code, you're welcome to contact any of the certified prtners:
- https://toolset.com/contractors/

#2810290

I tried in Chrome and Microsoft edge

#2810291

Hi, I just checked and the code is working correctly with the admin account I shared the credentials for. However, when I tried it with my personal admin account, it didn't load. It seems the code is functioning as expected, but could you help me understand why it isn't working with the other account?

#2810296

Update: The code is working properly on my main site. Thank you!