Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I want to create a search form that will only search a custom post typ WordPress

  • SOLVED

I want to create a search form that will only search a custom post type. And I want to control the template page that serves up the results. I've Googled and Googled and cannot get anything to work.

My site is built using the Underscores starter theme from Automattic.

Here's what I'm trying currently:

In the functions.php file:


function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'og_event' )
{
return locate_template('search-events.php'); // redirect to search-events.php
}
return $template;
}
add_filter('template_include', 'template_chooser');


And my modified search form:


<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Events"/>
<input type="hidden" name="post_type" value="og_event" />
<input type="submit" alt="Search" value="Search" />
</form>

Answers (7)

2020-11-11

Bob answers:

is your custom post type "publicly_queryable" ? It should be.

Did you check what this line is returning?
$post_type = get_query_var('post_type');
Is it array or single value?

Will you be able to provide access of your website?
or custom post type code, functions.php code and search template code?


Kyler Boudreau comments:

The search is returning results for the custom post type, but it's also searching other custom post types too. I want just the one specified.


Bob comments:

can we see your code of custom post type and search template code of your custom post type?

2020-11-11

Arnav Joy answers:

Is it the only search present on the site or there is another default search too?

If it is the only search then you can edit search.php file.


Arnav Joy comments:

Try this on functions.php

function mySearchFilter( $query ) {

if ( $query->is_search ) {
$query->set( 'post_type', 'og_event' );
}
return $query;
}
add_filter( 'pre_get_posts', 'mySearchFilter' );


Kyler Boudreau comments:

Hey Arnav, I added that to functions.php but it still shows all results.

I need to have more than one search form on the site. They will be searching different custom post types.

2020-11-11

Navjot Singh answers:

You can check out this page for building a custom search page to search for specific post types: https://www.smashingmagazine.com/2016/03/advanced-wordpress-search-with-wp_query/

2020-11-11

Mohamed Ahmed answers:

Hi Kyler,
Could you add this into functions.php


function ElDigital_Search_Filter( $query ) {

if ( $query->is_search ) {
$query->set( 'post_type', array('og_event') );
}
return $query;
}
add_filter( 'pre_get_posts', 'ElDigital_Search_Filter' );

2020-11-12

timDesain Nanang answers:

Hi Kyler,
there is nothing wrong with the code above.
i have tried it and it working perfectly

so, i assume the problem is not this part.


timDesain Nanang comments:

All Events


timDesain Nanang comments:

are you using pre_get_posts hook?


timDesain Nanang comments:

here is another approach:
1. set action to get_post_type_archive_link('og_event')
2. without passing post_type var

<form action="<?php echo get_post_type_archive_link('og_event');?>" method="get" role="search" id="searchform">
<input type="text" value="<?php echo get_search_query();?>" name="s" id="s" placeholder="Search Events" />
<input type="submit" alt="Search" value="Search" />
</form>

2020-11-12

Krishna Tiwari answers:

Hi,
Bellow step :
1- change search form (custom post 'project')
<h3>Search project</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search project"/>
<input type="hidden" name="post_type" value="project" /> <!-- // hidden 'products' value -->
<input type="submit" alt="Search" value="Search" />
</form>
2- add code into function.php
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'project' )
{
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');

3- Create custom search result template into theme
archive-search.php
Its working for me, if any further help, Please contact to me.
Thanks,
Krishna

2020-11-11

Cesar Contreras answers:

You can do it in your SEARCH.PHP file with the following code


$post_type = trim($_GET['post_type']);

switch ($post_type)
{
case 'og_event':
include 'search-events.php';
break;
default:
include 'search-default.php';
break;
}


This code I assume that you also have a template for any other search (default)