• Hi,

    I have a piece of custom code with the following query:

    $posts = query_posts('post_type=any&name='.$s);

    The result includes posts that have the ‘Exclude from search’ flag enabled. Is there a way to extend this query so it excludes these posts?

    Thanks!
    JP

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey @jpnl

    I’m having the same problem. My custom query worked for years and now is broken. Were you able to find a solution at all?

    Thanks!
    Joe

    Just was able to figure this out. Seems like you need to set the search exclude function to true. I added the following code to my search query and it works now.

    add_filter('searchexclude_filter_search', 'filterExclude', 10, 2);
    function filterExclude($exclude)
    {
    	return $exclude = true;
    }

    Thanks kabukinator,

    Mmm I don’t know how to add that to my piece of code. I am not much of a programmer myself. The code I have generates a list of posts and pages on the 404 page (it’s based on code from Yoast). This is the full code:

    $s = preg_replace(“/(.*)-(html|htm|php|asp|aspx)$/”,”$1″,$wp_query->query_vars[‘name’]);
    $posts = query_posts(‘post_type=any&name=’.$s);
    $s = str_replace(“-“,” “,$s);
    if (count($posts) == 0) {
    $posts = query_posts(‘post_type=any&s=’.$s);
    }
    if (count($posts) > 0) {
    echo “

    1. “;
      echo “Were you looking for one of the following pages?”;
      echo “

    “;
    } else {
    echo “

      “;
      }

      As a workaround I have changed the ‘post_type=any’ to ‘post_type=post’, so it excludes all pages, but I only want it to exclude posts/pages that have the search exclude flag ticked.

      Example of the result here: https://www.horecawebservice.nl/404

      Thanks
      JP

    Hey @jpnl

    From my understanding, you should be able to add my code snippet from above to your functions.php file.

    add_filter('searchexclude_filter_search', 'filterExclude', 10, 2);
    function filterExclude($exclude)
    {
    	return $exclude = true;
    }

    This should exclude any pages marked to be excluded with the Search Exclude plugin on all searches throughout your site. If that isn’t what you want, you can try adding it to just that 404 page template.

    P.S. If you aren’t using a child theme as of yet, this would be a good time to start. Good luck!

    Cool that works!

    I had found that code already in the documentation but didn’t think I needed it because the normal WordPress search already excluded the pages marked to be excluded. Only the custom code didn’t.

    Thank you for your help!

    JP

    @kabukinator just found that that filter isn’t a good solution as it also excludes these pages from the backend pages/posts view. :'(

    Plugin Contributor pronskiy

    (@pronskiy)

    Hello @jpnl,

    Filter is a slightly different thing, it does not affect custom queries, instead it is needed to modify behavior of search in a bulk mode.

    Anyway, I think updating query is more suitable for you. You basically need to add post__not_in param, something like this maybe:

    
    $posts = query_posts(['post_type'=>'any', 'name’ => $s, 'post__not_in' => get_option('sep_exclude', [])]);
    

    And since you have two query_posts, you need to add param in both.

    Please let me know how this works for you!

    Thanks, can’t get it to work, but it looks like there is already something wrong with the first part (as the $s remains empty). I’ll just drop this for now. Thank your for your time.

    JP

    It’s solved. Problem was indeed that the $s variable remained empty and that caused a list of every page and post on the site. I used different code now to fill $s with the last part of the url. The results are now correctly filtered by the search exclude plugin. So the problem was not this plugin.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Exlcude from custom query’ is closed to new replies.