Filter search results to the archive page it is being used on

Question

I am trying to create a shortcode with a search form that will allow the user to get filtered search results based on what page the search is being used on.

ie: If it is used in the blog archive, and user searches for ‘happy’, all posts with ‘happy’ will return.
If the user is in a category archive for ‘sun’ and searches for ‘happy’, only posts that have the term ‘happy’ that exist within that category archive will return, not from anywhere else – so returned posts have both the category AND term

I have used this code below in my functions.php, but whats happening is a) the filter doesn’t actually work, so searched results are still from everyone on the site, and b) the shortcode no longer shows up on the blog archive, just category archives.

Suggestions? Or is there a way to do this with the built in search that comes with a theme/wordpress (searchform.php or search.php)?

add_shortcode( 'wpbsearch', function() {
        ob_start();

        ?>
            <form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
    <input type="hidden" name="post_type" value="post">
​
    <?php if ( is_category() || is_tag() ) { ?>
        <?php $current_category = get_queried_object(); ?>
​
        <label>
            <span class="screen-reader-text"><?php echo esc_html_e( 'Search for:', 'client-name' ); ?></span>
            <input type="search" class="search-field"
                placeholder="<?php echo esc_html_e( 'Search', 'client-name' ); ?> <?php echo $current_category->name; ?>"
                value="<?php echo get_search_query() ?>" name="s"
                required/>
        </label>
​
        <?php if ( is_category() ) { ?>
​
            <input type="hidden" name="category_name" value="<?php echo $current_category->slug ?>">
​
        <?php } elseif ( is_tag() ) { ?>

            <input type="hidden" name="tag" value="<?php echo $current_category->slug ?>">
​
       <?php } else { ?>
​
        <label>
            <span class="screen-reader-text"><?php echo esc_html_e( 'Search for:', 'client-name' ); ?></span>
            <input type="search" class="search-field"
                placeholder="<?php echo esc_html_e( 'Search', 'client-name' ); ?>"
                value="<?php echo get_search_query() ?>" name="s"
                required/>
        </label>
        <?php } ?>
        <button type="submit" class="search-submit" value="" />
        <span class="screen-reader-text"><?php echo esc_html_e( 'Search', 'client-name' ) ?></span>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 23.92 23.93" aria-hidden="true" class="search-submit-icon"><path d="M17,14.69l.39.14a2.51,2.51,0,0,1,.8.61l5.07,5.07a1.93,1.93,0,0,1,.57,1.83,1.89,1.89,0,0,1-1,1.33,1.91,1.91,0,0,1-2-.07,2.56,2.56,0,0,1-.37-.3l-5.24-5.23a2.15,2.15,0,0,1-.53-.88.93.93,0,0,0,0-.13,9.33,9.33,0,0,1-9.57.63,8.83,8.83,0,0,1-4.08-4A9.35,9.35,0,0,1,14.5,1.55a9,9,0,0,1,4,6A9.25,9.25,0,0,1,17,14.69ZM9.35,16.35a7,7,0,1,0-7-7A7,7,0,0,0,9.35,16.35Z"/></svg>
    </button>
        <?php
        return ob_get_clean();
    } } );```
0
, user177973 4 years 2019-11-06T14:24:05-05:00 0 Answers 68 views 0

Leave an answer

Browse
Browse