I've tried a couple of methods but I cannot seem to filter custom post_types from my search results and was hoping someone could help.
I have installed "Job Manager" and created 4 jobs which have a custom post_type = 'jobman_job'
I tried to create a manual search form and set a hidden value of post_type = jobman_job but it still returned all posts.
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> <input type="text" name="s" id="s" value=""/> <input type="hidden" name="post_type" value="jobman_job" /> <input type="submit" id="searchsubmit" value="Search" /> </form>
I then tried creating a custom search page and redirecting the search to this page as follows (i.e added page_id hidden field):
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> <input type="text" name="s" id="s" value=""/> <input type="hidden" name="page_id" value="123" /> <input type="hidden" name="post_type" value="jobman_job" /> <input type="submit" id="searchsubmit" value="Search" /> </form>
And then in the custom search page, I added the following code (as per wordpress guide - http://codex.wordpress.org/Creating_a_Search_Page) and I added the post_type of jobman_job to the query array:
global $query_string; $query_args = explode("&", $query_string); $search_query = array('post_type' => 'jobman_job'); foreach($query_args as $key => $string) { $query_split = explode("=", $string); $search_query[$query_split[0]] = urldecode($query_split[1]); } // foreach $search = new WP_Query($search_query);
And it still displays all posts...
What am I doing wrong? I have checked the post_type column in the wp_posts table and I have 4 unique entries...so they are there...
Any Insight?
I simply left the html as is:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> <input type="text" name="s" id="s" value=""/> <input type="hidden" name="post_type" value="jobman_job" /> <input type="submit" id="searchsubmit" value="Search" /> </form>
and added the following to my functions.php
function mySearchFilter($query) { if (isset($_GET['post_type']) && $_GET['post_type'] == 'jobman_job') { $post_type = 'jobman_job'; } else { $post_type = 'any'; } if ($query->is_search) { $query->set('post_type', $post_type); }; return $query; }; add_filter('pre_get_posts','mySearchFilter');
我尝试了几种方法,但似乎无法从搜索结果中过滤自定义 post_types ,希望有人能提供帮助。
我已经安装了"作业管理器"并创建了4个作业,这些作业具有自定义的 post_type ='jobman_job'
我试图创建一个手动搜索表单,并将隐藏值设置为 post_type = jobman_job ,但它仍返回所有帖子。
< form role =" search" method =" get" id =" searchform" action ="<?php echo home_url('/'); ?>"> <输入类型="文本" name =" s" id =" s" value ="" /> < input type =" hidden" name =" post_type" value =" jobman_job" /> <输入类型=" submit" id =" searchsubmit" value ="搜索" /> < / form>
然后我尝试创建自定义搜索页面,并将搜索重定向到此页面,如下所示(即添加了< code> page_id 隐藏字段):
< form role =" search"方法=" get" id =" searchform" action ="<?php echo home_url('/');?>"> <输入类型="文本" name =" s" id =" s" value ="" /> < input type =" hidden" name =" page_id" value =" 123" /> < input type =" hidden" name =" post_type" value =" jobman_job" /> <输入类型=" submit" id =" searchsubmit" value ="搜索" /> < / form>
然后在自定义搜索页面中,我添加了以下代码(根据wordpress指南- http://codex.wordpress.org/Creating_a_Search_Page ),我添加了 post_type 到查询数组:
global $ query_string; $ query_args = explode("&",$ query_string); $ search_query = array(‘post_type’=>‘jobman_job’); foreach($ query_args as $ key => gt $ string){ $ query_split = explode(" =",$ string); $ search_query [$ query_split [0]] = urldecode($ query_split [1]); } // foreach $ search = new WP_Query($ search_query);
它仍然显示所有帖子...
我在做什么错?我已经检查了 wp_posts 表中的 post_type 列,我有4个唯一的条目...所以它们在那里。 ..
有什么见解?
我只是离开了html
< form role =" search" method =" get" id =" searchform" action ="< ?php echo home_url('/');?>"> <输入类型="文本" name =" s" id =" s" value ="" /> < input type =" hidden" name =" post_type" value =" jobman_job" /> <输入类型=" submit" id =" searchsubmit" value ="搜索" /> < / form>
并将以下内容添加到我的function.php
函数mySearchFilter($ query){ if(isset($ _ GET ['post_type'])&& $ _GET ['post_type'] =='jobman_job'){ $ post_type ='jobman_job'; } else { $ post_type =‘any’; } if($ query-> is_search){ $ query-> set(’post_type’,$ post_type); }; 返回$ query; }; add_filter('pre_get_posts','mySearchFilter');