$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Display Search Results by tag_ID in my search.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Display Search Results by tag_ID in my search.php

matteradmin9PV0评论

I have my search.php working fine. But I'd like to display the search results in my search.php filtered by tag_ID when is coming from another search form. I've tried several things like: wp_parse_str, new WP_Query( {parameters here} ), etc. Basically I don't know how to modify the query :(

How can I modify the main query in order to do that?

Thank you very much for your help!

PD: I've been reading several post here but no one answered my question.

This is my form code:

<form role="search" method="get" class="form-inline cp-form-search search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="s" class="search-field form-control p-2"  placeholder="<?php echo esc_attr_x( 'Search '.$venue_archive_title, 'placeholder' ); ?>" aria-label="Search" value="<?php echo get_search_query(); ?>" name="s">
                    <input type="hidden" value="restaurants" name="venue_type_search" id="venue_type_search" />
                    <input type="hidden" value="title" name="orderby" id="orderby" />
                    <input type="hidden" value="ASC" name="order" id="order" />
                    <button type="submit"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/search-white.svg" height="22"></button>
                </form>

I'd like to trigger the new query with my "venue_type_search" hidden input.

Thanks!

I have my search.php working fine. But I'd like to display the search results in my search.php filtered by tag_ID when is coming from another search form. I've tried several things like: wp_parse_str, new WP_Query( {parameters here} ), etc. Basically I don't know how to modify the query :(

How can I modify the main query in order to do that?

Thank you very much for your help!

PD: I've been reading several post here but no one answered my question.

This is my form code:

<form role="search" method="get" class="form-inline cp-form-search search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="s" class="search-field form-control p-2"  placeholder="<?php echo esc_attr_x( 'Search '.$venue_archive_title, 'placeholder' ); ?>" aria-label="Search" value="<?php echo get_search_query(); ?>" name="s">
                    <input type="hidden" value="restaurants" name="venue_type_search" id="venue_type_search" />
                    <input type="hidden" value="title" name="orderby" id="orderby" />
                    <input type="hidden" value="ASC" name="order" id="order" />
                    <button type="submit"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/search-white.svg" height="22"></button>
                </form>

I'd like to trigger the new query with my "venue_type_search" hidden input.

Thanks!

Share Improve this question edited Feb 12, 2019 at 22:51 Daniel Garrido asked Feb 8, 2019 at 17:46 Daniel GarridoDaniel Garrido 33 bronze badges 1
  • You can use pre_get_posts. But, "another search form" - where's the code? – Sally CJ Commented Feb 10, 2019 at 13:23
Add a comment  | 

1 Answer 1

Reset to default 0

Filter it using pre_get_posts, like this:

function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['venue_type_search'])) {
            $venue_type = $_GET['venue_type_search'];

            $query->set('tag',array($venue_type));

        }       
    }
    return $query;
}
add_filter('pre_get_posts','searchfilter');
Post a comment

comment list (0)

  1. No comments so far