Horje
I want to create a search form that will only search a custom post typ - Wordpress Solution
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>


Solution - 1

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?


Solution - 2

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.


Solution - 3

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/


Solution - 4

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' );


Solution - 5

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.


Solution - 6

Hi, Bellow step : 1- change search form (custom post 'project')

Search project

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


Solution - 7

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)





Wordpress

Related
ACF: How to programmatically Update Flexible Content Field? - Wordpress Solution ACF: How to programmatically Update Flexible Content Field? - Wordpress Solution
Wordpress posts per hour - Wordpress Solution Wordpress posts per hour - Wordpress Solution
Website recently received a Website recently received a
The little little question... where is the url of the taxo... ? - Wordpress Solution The little little question... where is the url of the taxo... ? - Wordpress Solution
Events calendar description dont display on mobile divices - Wordpress Solution Events calendar description dont display on mobile divices - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
23