Wordpress 搜索中的分类筛选未显示分配的帖子
我有一个自定义类型帖子的搜索表单,它有一个普通的搜索输入和类别过滤器(我使用了自定义分类法,因为我希望它像带有类别的帖子一样) 下面是我如何在functions.php中创建名为Wordpress 搜索中的分类筛选未显示分配的帖子,wordpress,search,filter,custom-post-type,taxonomy,Wordpress,Search,Filter,Custom Post Type,Taxonomy,我有一个自定义类型帖子的搜索表单,它有一个普通的搜索输入和类别过滤器(我使用了自定义分类法,因为我希望它像带有类别的帖子一样) 下面是我如何在functions.php中创建名为custom的分类法: function themes_taxonomy() { $labels = array( 'name' => _x( 'Categorias Portafolio', 'taxonomy general name', 't
custom
的分类法:
function themes_taxonomy()
{
$labels = array(
'name' => _x( 'Categorias Portafolio', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Categoria Portafolio', 'taxonomy singular name', 'textdomain' ),
'menu_name' => __( 'Categorias Portafolio', 'textdomain' )
);
$args = array(
'hierarchical' => true,
'labels' => $labels, // display name
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'public' => true,
'rewrite' => array( 'slug' => 'proyecto' ) // This controls the base slug that will display before each term
);
register_taxonomy( 'custom', 'proyecto' , $args);
}
add_action( 'init', 'themes_taxonomy');
我在functions.php中创建了自定义的post类型proyecto
function wpdocs_codex_book_init()
{
$labels = array(
'name' => __('Proyectos'),
'singular_name' => __('Proyectos'),
'menu_name' => __('Proyectos'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title' ),
'menu_icon' => 'dashicons-building',
'exclude_from_search' => false,
'can_export' => true,
'taxonomies' => array( 'custom' )
);
register_post_type( 'proyecto', $args);
}
add_action( 'init', 'wpdocs_codex_book_init' );
<?php
if(have_posts())
{
if (get_post_type() == 'proyecto')
{
while (have_posts())
{
the_post();
/* content */
}
}
else
{
if (get_post_type() == 'post')
{
while (have_posts())
{
the_post();
/* content */
}
}
else
{
?>
<div class="ml-3">No se encontraron publicaciones.</div>
<?php
}
}
}
else
{
?>
<div class="ml-3">No se encontraron publicaciones.</div>
<?php
}
?>
这是搜索表单以下拉列表的形式获取输入和自定义分类(类别)的方式:
<form action="<?php echo site_url('/');?>" method="get" class="search-form searchandfilter justify-content-between" id="searchbar">
<div class="row">
<div class="offset-lg-2 col-lg-6">
<label class="search-bar">
<div class="btn-search"><i class="fa fa-search"></i></div>
<input type="text" name="s" class="search-txt" placeholder="Búsqueda" value="" />
<input type="hidden" name="post_type" value="proyecto"/>
</label>
</div>
<div class="col-lg-4">
<?php
$tax_terms = get_terms('custom', array('hide_empty' => '0'));
?>
<select name="cat" id="cat" class="postform custom-select" name="categoryfilter">
<option value="0">Categoría</option>
<?php
foreach ( $tax_terms as $tax_term )
{
?>
<option value="<?php echo $tax_term->term_id;?>" data-categoria="<?php echo $tax_term->name;?>"><?php echo $tax_term->name;?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="row justify-content-end cont-btn">
<input type="submit" value="Buscar" class="btn-submit btn-main-aqua">
<div class="btn-submit btn-main-aqua" onclick="displaymessage();reset()">Vaciar Campos</div>
</div>
</form>
但它不起作用。。。有人能告诉我我是否遗漏了什么吗?我还不熟悉wordpress…用
自定义代替猫怎么样?我认为你应该在这方面努力。事实上,这就是问题所在!我还将选项的值更改为$tax\u term->name
,因为id似乎不起作用。。。你知道为什么会这样吗?非常感谢。
function custom_cpt_search( $query )
{
if ( is_search() && $query->is_main_query() && $query->get( 's' ) )
{
$query->set('post_type', array('proyecto'));
}
return $query;
};
add_filter('pre_get_posts', 'custom_cpt_search');