0

So i have searched almost everywhere and tried at least a dozen different ways to accomplish a custom wp search. Hopefully i can verbalize what im trying to do well..here goes.

I have a content page - videos - with the main video as the first div, and then under it i have a div containing previous videos. I also have other content type in other pages(articles, guides etc - all custom post_types. they are also child pages of a main page). At the top i have a search bar with wp native search. I want the search behavior to behave in this way:
* when i search for something, it searches for specific post types (articles guides etc NOT blogs) .
* it doesnt redirect and returns the data (either page refresh or like a response object that i can use to populate the page), or if it has to, it redirects to my custom template; if it redirects to a custom template, i need to somehow keep the search query in hand.

What im trying to accomplish, making it seem to the user, that they did not navigate to another page, they stayed on the same page and either the results just popped up, or the page refreshed with the results.

Things i have tried:
* form with multiple hidden inputs with the value of the custom post type that i want to display - goes to search page with only one result showing;
* custom template chooser - if the post type is x display y template - cant figure out a way to keep the search string and the url similar to what it was.
* running a wp query with the post types in an array, the search string and the post per page to -1 - cant figure out how to trigger that function on search and how to return the data to put together the html (return the html? but still not sure how to trigger the function on search(event))

Hopefully what i explained here makes sense. these are the links to SO/SE of the methods that i have tried.
Template chooser - How to create a custom search for custom post type? .

Ive created a custom search template for each type of post to make it seem like the user ended up on the same page (no luck though)

function template_chooser($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'article' ) {
    return locate_template('article-search.php');
  } else if ( $wp_query->is_search && $post_type == 'guides-ebooks' ) {
      return locate_template('guides-ebooks-search.php');
  } else if ( $wp_query->is_search && $post_type == 'greenhouse-videos' ) {
      return locate_template('videos-search.php', true);
  } else if ( $wp_query->is_search && $post_type == 'podcasts' ) {
      return locate_template('podcasts-search.php');
  } else if ( $wp_query->is_search && $post_type == 'quizzes-tools' ) {
      return locate_template('quizzes-tools-search.php');
  }
  return $template;   
}
add_filter('template_include', 'template_chooser');

.

and this is the code to keep the search url similar (ive tried tried with and without this fn) .

function wpb_change_search_url() {
     if ( is_search() && ! empty( $_GET['s'] ) ) {
         wp_redirect( home_url( "/search/?s=" ) . urlencode( get_query_var( 's' ) ) );
         exit();
     }   
 }
 add_action( 'template_redirect', 'wpb_change_search_url' );

.

this is the custom query method that i have, but couldnt figure out how to trigger it on search:

function custom_search($echo = true) {
    $args = array(
        'post_type' => array(
            'article',
            'guides-ebooks',
            'greenhouse-videos',
            'podcasts',
            'quizzes-tools'
        ),
        's' => get_search_query(),
        'post_per_page' => -1
    );

    $form ='<form action="' . custom_search() . '">
        <div>
            <input type="text" value="' . get_search_query() . '" />
            <input type="submit" id="searchsubmit" value="Search" />
        </div>
    </form>';

    $query = new WP_Query($args);

    if ( $query->have_posts() ) :
        while ($query->have_posts() ) : $query->the_post();
            $output .= '<h1>' . get_the_title() . '</h1>';
        endwhile;

    else :
        $output = '<h1>Nothing found yo</h1>';
    endif;

    return $output;
    wp_reset_postdata();

    if ($query->have_posts()) {
        return $query;
    } else {
        echo "No Posts Found";
    }
  }

And this is the try with the multiple hidden inputs with the post types:

<?php echo
        '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '">

            <div>

            <label for="s">Search for:</label>

            <input type="text" value="' . get_search_query() . '" name="s" id="s" />

            <input type="hidden" value="article" name="post_type[]" />

            <input type="hidden" value="guides-ebooks" name="post_type[]" />

            <input type="hidden" value="greenhouse-videos" name="post_type[]" />

            <input type="hidden" value="podcasts" name="post_type[]" />

            <input type="hidden" value="quizzes-tools" name="post_type[]" />

            <input type="submit" id="searchsubmit" value="Search" />

            </div>

            </form>'
        ?>

Any help would be greatly appreciated. Thanks!

(if that didnt make sense, this is the question - How do I do two customized searches, in separate sections of the site? I know how to customize the look and feel of search.php, but how do I effectively have two? For instance, http://example.com/section1/search?s=term and http://example.com/section2/search?s=term)

  • I always just manipulate the search.php in Theme as I need it. Maybe not helpful sorry – seot Aug 30 '18 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.