将自定义帖子类型/字段添加到Wordpress搜索

问题描述:

我正在寻找一种简单的方法来将自定义帖子类型自定义字段添加到它们中的自定义帖子类型,以便在我的Wordpress网站中搜索功能。我很好地以编程方式或使用插件,但我尝试过的插件没有意义。将自定义帖子类型/字段添加到Wordpress搜索

目前,当我搜索时,只有我的自定义帖子类型的标题会出现在搜索中,但我希望能够通过类似于我的帖子类型,帐户(例如关联的人)用那个账号。基本上我希望能够键入Johnny Appleseed,即使他的账户的标题不是他的名字,他的名字也是一个名为contact_name的自定义字段。

这怎么办?

添加此“‘query_var’=>真”

添加上面的代码,当你创建自定义后的类型。

更多信息遵循此:https://codex.wordpress.org/Function_Reference/register_post_type

,如果您使用下面的代码的任何插件的用户


add_filter('pre_get_posts', 'tgm_cpt_search'); 
/** 
* This function modifies the main WordPress query to include an array of post types instead of the default 'post' post type. 
* 
* @param mixed $query The original query 
* @return $query The amended query 
*/ 
function tgm_cpt_search($query) { 
if ($query->is_search) 
$query->set('post_type', array('post', 'movies', 'products', 'portfolio')); 
return $query; 
}; 
+0

我正在使用插件,我应该试着编辑插件吗?另外,这是否适用于所有自定义字段? – Hunter

+0

if you can edit the plugin then good ... 或使用波纹管代码 ------------------ add_filter('pre_get_posts','tgm_cpt_search'); /** *此功能修改主要WordPress查询,以包含帖子类型数组而不是默认的“发布”帖子类型。 * * PARAM混合$查询的原始查询 *返回$查询修改后的查询 */ 功能tgm_cpt_search($查询){ 如果($查询 - > is_search) $查询 - >置( 'post_type',数组('后','电影','产品','组合')); return $ query; }; –

+0

迄今为止还没有工作...我会尝试一些不同的东西 – Hunter

你不需要的插件,你需要修改(或创建)在你的主题的根目录search.php。模型,可以archive.php后,主循环中

<?php while (have_posts()) : the_post(); ?> 
    //output post information here 
<?php endwhile; ?> 

您可以打开get_post_type(),并显示在此基础上不同的信息。

<?php while (have_posts()) : the_post(); ?> 
    if(get_post_type() == "custom_type") 
    { 
    //load custom fields and output here 
    } 
    else 
    { 
    //output normal post info here ala the_title(), the_excerpt(), etc... 
    } 
<?php endwhile; ?>