php 自定义帖子类型的搜索表单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 自定义帖子类型的搜索表单相关的知识,希望对你有一定的参考价值。

https://wordpress.stackexchange.com/questions/248983/using-standard-search-function-with-custom-post-type
https://gist.github.com/galengidman/8b84770a2dcc9abb8bfe


1) THE HTML THAT DISPLAYS THE FORM with the custom post type as hidden value
this will diplay results of the custom post type only , it wont match
any other keywords that might exist in other pages
this is only needed for exclusive custom post type search only
<form class="search" action="<?php echo home_url( '/' ); ?>">
  <input type="search" name="s" placeholder="Search&hellip;">
  <input type="submit" value="Search">
  <input type="hidden" name="post_type" value="your_post_type_here">
</form>


// THIS IS OPTIONAL:
2) Create search-your_custom_post_type_here.php
Add the following:

<?php
// store the post type from the URL string
$post_type = $_GET['post_type'];
// check to see if there was a post type in the
// URL string and if a results template for that
// post type actually exists
if ( isset( $post_type ) && locate_template( 'search-' . $post_type . '.php' ) ) {
  // if so, load that template
  get_template_part( 'search', $post_type );
  
  // and then exit out
  exit;
}










以上是关于php 自定义帖子类型的搜索表单的主要内容,如果未能解决你的问题,请参考以下文章

php 搜索自定义帖子类型

php 自定义帖子类型搜索

php 自定义帖子类型搜索

php 在Wordpress搜索中包括所有自定义帖子类型

停止显示自定义帖子类型的 wordpress 搜索

从 Wordpress 取消注册自定义帖子类型