• Resolved thinkwired

    (@thinkwired)


    I would like to use the default relevanssi results for my site wide search but, I would also like a more specific search if a variable is passed through the search url — for instance http://mysite.com/?somevariable=true&s=test

    I know how to get the passed variable and create the conditional but, how can I modify the search results to display only pages of a specific static parent if that condition is true? I’m looking through your code example but all I see are functions that will alter the main search as well. I want the main search to remain the same but also display results based on the hardcoded parent ID if the “somevariable=true.”

    Is this possible? Can you have two sets of results on the same search.php where only one is shown depending on some condition being true?

    https://ww.wp.xz.cn/plugins/relevanssi/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter thinkwired

    (@thinkwired)

    if ($somevariable=='true') {
    display custom results where parent ID = 123
    } else {
    display normal relevanssi results
    };
    Plugin Author Mikko Saari

    (@msaari)

    There’s the WordPress query parameter post_parent, which would be the easy way to do this, but Relevanssi doesn’t support it – you’re the first one to ever ask about it. Maybe I’ll add it in a future version.

    Meanwhile, you’re best off using relevanssi_hits_filter. Something like this (I didn’t test it so no guarantees it actually works, but you get the idea):

    add_filter('relevanssi_hits_filter', 'rlv_post_parent', 10);
    function rlv_post_parent($hits) {
        global $wp_query;
        if (isset($wp_query->query_vars['somevariable'])) {
            $valid_posts = array();
            foreach ($hits[0] as $hit) {
                if ($hit->post_parent == 123) $valid_posts[] = $hit;
            }
            $hits[0] = $valid_posts;
        }
        return $hits;
    }

    Something like this. You need to introduce your query variable to WordPress with query_vars, otherwise WordPress wil clean it out.

    Thread Starter thinkwired

    (@thinkwired)

    I don’t seem to be able to make it work. I think the code is as you’ve explained. Ive updated with my own actual variables.

    function themeslug_query_vars( $searchdept ) {
      $searchdept[] = 'custom_query_var';
      return $searchdept;
    }
    add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
    add_filter('relevanssi_hits_filter', 'rlv_post_parent', 10);
    function rlv_post_parent($hits) {
        global $wp_query;
        if (isset($wp_query->query_vars['searchdept'])) {
            $valid_posts = array();
            foreach ($hits[0] as $hit) {
                if ($hit->post_parent == '32') $valid_posts[] = $hit;
            }
            $hits[0] = $valid_posts;
        }
        return $hits;
    }

    The search url is as follows http://mysite.com/?s=search+term&searchdept=true

    Any ideas?

    Plugin Author Mikko Saari

    (@msaari)

    I’d start debugging this piece by piece. Is the code running? Is the searchdept parameter available? Does the post_parent if trigger? Where does the code fail?

    Thread Starter thinkwired

    (@thinkwired)

    You are adding this code to the function.php file correct?

    Thread Starter thinkwired

    (@thinkwired)

    If I change return $hits; to echo 'test';

    Test is displayed on the page so the function is running.

    If I var_dump $hits, it displays the array but that array includes things other than posts with a parent of 32.

    It seems as though this bit of code is not true

    if (isset($wp_query->query_vars['searchdept'])) {
            $valid_posts = array();
            foreach ($hits[0] as $hit) {
                if ($hit->post_parent == '32') $valid_posts[] = $hit;
            }
                	echo '<h1>test</h1>';
        }

    I have no idea why.

    Thread Starter thinkwired

    (@thinkwired)

    Yup, If I remove this condition it works.

    if (isset($wp_query->query_vars['searchdept'])) {

    Strange. The variable is in the url with a value. Any ideas?

    Thread Starter thinkwired

    (@thinkwired)

    Changing that condition to this worked

    if ($_GET["searchdept"] == "true") {

    Plugin Author Mikko Saari

    (@msaari)

    Ah, this is incorrect:

    function themeslug_query_vars( $searchdept ) {
      $searchdept[] = 'custom_query_var';
      return $searchdept;
    }
    add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );

    It should be:

    function themeslug_query_vars( $qv ) {
      $qv[] = 'searchdept';
      return $qv
    }
    add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );

    Now the original code should work as well.

    Thread Starter thinkwired

    (@thinkwired)

    ugh, that was s dumb mistake. late night. thanks again.

    Thread Starter thinkwired

    (@thinkwired)

    Im passing the searchdept value through a hidden field in the search form so, I guess I dont need that portion of the code anymore since the GET method works.

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

The topic ‘Coditional results based on passing variable in URL?’ is closed to new replies.