mercredi 16 décembre 2020

Custom search results page for specific taxonomy only

I am seeing variants of what I am after, but all the results seem a lot more complicated than what I need.

My wordpress website uses relevanssi to search for everything from pages to posts but I want it to not include any posts from my custom taxonomy (called 'guidelines') within the 'Members zone' of the site which requires the user to log in.

I want to create a search bar as a shortcode that I can dump throughout the 'Members Area' that searches through the 'guidelines' taxonomy only, and takes you to a 'Members zone' search results page.

Here is what I'm working with currently.

In my functions.php folder I have added:

add_shortcode( 'guideline_search', function() {

    ob_start();
    ?>
    <form class="members-search" role="search" action="<?php echo site_url( '/members-zone/members-search/' ); ?>" method="get" id="searchform">
        <input type="text" name="s" placeholder="Search the Members Zone"/>
        <input type="hidden" name="post_type" value="guidelines" /> <!-- // hidden 'guidelines' value -->
        <input type="submit" alt="Search" value="Search" />
    </form>
    <?php
    return ob_get_clean();
});

I have created a file called 'archive-search.php' with the following code:

<?php  
$search_field = $_GET['search_field'];
$my_select_taxonomy = $_GET['guidelines'];

$args = array(
'post_type' => 'post',
'tax_query' => array(
    array(
        'taxonomy' => $my_select_taxonomy,
        'field' => 'slug',
        'terms' => $search_field,
    )
)
);
$query = new WP_Query( $args );

?>

Is there an easier way to do this, or even, a better way to use relevanssi? I couldn't find a way to have it work with multiple search functionalities.




Aucun commentaire:

Enregistrer un commentaire