0

How can I modify this code to make the search box include tags and meta

I would like the search in same box

if($_GET['s'] && !empty($_GET['s']))
{
    $text = $_GET['s'];
}

if($_GET['type'] && !empty($_GET['type']))
{
    $type = $_GET['type'];
}

$args = array(
    'post_type' => $type,
    'post_status'    => 'publish',
    's' => $text,
);

$query = new WP_Query($args);
4
  • if you use an input with the name post_type then you don't need a new WP_Query or a special $_GET['type'], it'll automatically generate URLs with post_type=xyz in the URL that are picked up by WordPress. This also means that a lot of the existing answers will work for you (because you discard the main query and create a new WP_Query those existing answers might not work for you, and you've made your search pages twice as slow).
    – Tom J Nowell
    Jul 30, 2022 at 16:31
  • Also note that search is built to search posts, you'll need to be more specific about what you mean by meta and tags, e.g. are you searching for tags themselves and want to display a tag as a search item? Or do you mean posts in a tag that you've searched for? Searching meta will be very slow/expensive, and get more expensive as you add more content to your site. There's also 2 bugs in your code, if $_GET['type'] isn't set the $type variable is undefined, yet you use it in the array even if it's undefined, which will give you PHP warnings and notices. I see no search box code in the Q
    – Tom J Nowell
    Jul 30, 2022 at 16:32
  • no i need to search for post have custom meta Jul 30, 2022 at 16:34
  • like any post have book meta will show in result Jul 30, 2022 at 16:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.