How to check if search query contain some characters? For example I would like to check if search query contain "/".

I tried this answer but it is not working for me. Just to say I am using custom search base.



where "myseachbase" is my custom search base. What I need is way to check if search query has "/", in some cases it is at the end of search query, for example:


How to check for that ending "/" in search URL?


Problem is that I have some custom URLs like:


They are practically search results, even visitor open them by clicking on link(s), not by using normal site search.

I need to make them indexable only if:

  • there are search results
  • it is first search results page, not for any other /page/x
  • search query has specific keyword(s)
  • search results are correct and usefull results
  • plus to add some quality content prepared by editor based on search keyword

After I have done that part, I have nice quality content, custom text based on search query + some images (images are search results) on URLs like:


but, if you add "/" at the you will get same page, but another URL, which will met conditions to be indexable too. So, I need way to put "noindex" tag in case if it is that duplicate URL, URL with "/" at the end, because I have no option to create canonicial URL. For that I need something to check for "/".

  • I think "/" in a URL is not passed in the query string; I think you need something else. Can you explain why do you need that and what is your purpose?
    – cybmeta
    Commented Nov 24, 2016 at 10:48
  • @cybmeta I updated my question with detailed explanation, regards. Commented Nov 25, 2016 at 10:00
  • See muy answer, what you need is to set the canonical URL in the search results page. You don't need to detect "/" at the end of the URL and you don't need to set "noindex".
    – cybmeta
    Commented Nov 25, 2016 at 10:22

2 Answers 2


After the question's update, I think that you need to set the canonical URL:

add_action( 'wp_head', 'cyb_search_results_canonical_URL' );
function cyb_search_results_canonical_URL() {
    if( is_search() ) {
        $link = get_search_link();
        echo '<link rel="canonical" href="' . esc_url( $link ) . '">';

And your problem with duplicated content is fixed.

  • Thank you, that is great, yes I needed get_search_link();. Great answer. Commented Nov 25, 2016 at 10:43

You'll need to hook onto the search query through pre_get_posts. The following snippet will search for / in the query.

function search_query($query){
    if (!is_admin() && $query->is_main_query() && is_search()){
        $search = $query->query_vars['s'];
        if (strpos('/', $search) !== false){
            // Found '/' in search query
  • Not working, acctually, it never found "/", even if I type it in search query. Commented Nov 24, 2016 at 11:28
  • It might be the query isn't the main query - try removing the && $query->is_main_query() part of the if-statement.
    – Emil
    Commented Nov 24, 2016 at 11:47
  • I rtied to remove all this if (!is_admin() && $query->is_main_query() && is_search()){ and to add if (strpos('/', $search) !== false){ echo 'Found'; } else { echo 'Not found'; }, but I always get "Not found", even if I add "/" at the end of search URL. I also tried to change strpos('/', $search) to strpos('test', $search), and to search for "test", but always it is "Not found". Like search query is empty or like it even does not look at search query at all. Commented Nov 24, 2016 at 16:36
  • Can you post here what echo $search; outputs?
    – Emil
    Commented Nov 25, 2016 at 8:10
  • I tested search output with: echo 'Search query is: ' .$search. '';, and output is blank, nothing in my case, even there is search keyword in URL, and there are search results. Commented Nov 25, 2016 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.