Disabled pages and posts shown in search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Disabled pages and posts shown in search results

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 4 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #41437
    Heizpad
    Heizpad
    Participant

    Hi!

    I use a membership plugin (Digimember) on my website. This plugin enables access to some pages and posts for members who bought a specific membership and disables access to other specific pages and posts.

    When using Ajax Search Pro all pages and posts are loaded inside the live search results. Of course disabled pages and posts should not be in the search results. Is it possible to remove those disabled pages and posts in the search results?

    Thank you

    #41440
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    It is very likely possible. I have googled a bit and found their API, so in theory it is possible to use a custom code to filter the posts not accessible for the current user.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_filter( 'asp_results', 'asp_digimember_access_filter', 10, 1 );
    function asp_digimember_access_filter( $results ) {
      foreach ($results as $k=>&$r) {
    	if ( isset($r->post_type) ) {
    		if ( digimember_currentUserAccessDenied($r->post_type, $r->id) )
    			unset($results[$k]);
    		}
    	}
      } 
      return $results;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #41441
    Heizpad
    Heizpad
    Participant

    Hi Ernest,

    wow, thank you for your efforts.

    When I added your provided code my whole site crashed. Then I removed one closing brace and now it works. Just to make sure, I used this code instead:

    add_filter( 'asp_results', 'asp_digimember_access_filter', 10, 1 );
    function asp_digimember_access_filter( $results ) {
      foreach ($results as $k=>&$r) {
    	if ( isset($r->post_type) ) {
    		if ( digimember_currentUserAccessDenied($r->post_type, $r->id) )
    			unset($results[$k]);
    		}
      } 
      return $results;
    }

    Am I right with my assumption that in your code there was one brace too much?

    As I said, it works now and doesn’t return disabled search results. However, it still suggests me “Did you mean: ” and then search input suggestions for disabled pages are displayed. Is it possible to disable search suggestions for disabled pages and posts as well?

    Thank you a lot again for your help!

    #41442
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You are welcome!

    Yes, indeed there is an error in the code (as I couldn’t test it), you have corrected it properly, you can also use this version with the extra bracket for nicer encapsulation:

    add_filter( 'asp_results', 'asp_digimember_access_filter', 10, 1 );
    function asp_digimember_access_filter( $results ) {
      foreach ($results as $k=>&$r) {
    	if ( isset($r->post_type) ) {
    		if ( digimember_currentUserAccessDenied($r->post_type, $r->id) ) {
    			unset($results[$k]);
    		}
    	}
      } 
      return $results;
    }

    You can disable the keyword suggestions here: https://documentation.ajaxsearchpro.com/autocomplete-and-keyword-suggestions/keyword-suggestions

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #41444
    Heizpad
    Heizpad
    Participant

    Hi again.

    Thanks for that link, but I don’t want to completely disable keyword suggestions. I just want to remove suggestions from pages and posts that an user has no access to.

    For example I have a page with the title “Membership only page” and access to this page is blocked via Digimember for all users that have no membership.

    When I, as a user without a membership, start typing “Member” it will automatically suggest me in the input bar “Membership only page” but of course return no results in the live results.

    Could you please provide a code to also remove those type of search suggestions?

    Thank you a lot again!

    #41460
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I had to look at the source code for this, and I’m afraid this may not be possible for that feature. The restricted post IDs are not known before the suggestions query is executed, and afterwards the post IDs are not kept, so there is no way to check which title belongs to which post.

    All I can do now is to change the code a bit and add a hook for a future release to make this possible. It should not be too complicated.

    If you want, you can add temporary FTP access and I can try to make the modification for you with the custom code as well, at least I can test it on an actual use case before adding to the next patch.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.