Php 需要帮助为cpt创建自定义搜索吗
搜索应该只显示相关的分类法帖子,但它会占用整个cpt帖子 我从头开始创建一个工作板,我创建了一个名为jobs的自定义帖子类型,创建了分类法作为job location,在functions.php中定义了函数,创建了一个搜索页面,如下所示: 步骤1:创建名为作业的CPTPhp 需要帮助为cpt创建自定义搜索吗,php,wordpress,Php,Wordpress,搜索应该只显示相关的分类法帖子,但它会占用整个cpt帖子 我从头开始创建一个工作板,我创建了一个名为jobs的自定义帖子类型,创建了分类法作为job location,在functions.php中定义了函数,创建了一个搜索页面,如下所示: 步骤1:创建名为作业的CPT //added cpt function codex_custom_init() { register_post_type( 'Jobs', array( 'labels' => ar
//added cpt
function codex_custom_init() {
register_post_type(
'Jobs', array(
'labels' => array('name' => __( 'Jobs' ), 'singular_name' => __( 'Jobs' ) ),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'has_archive' => 'jobs',
'supports' => array('title', 'editor', 'thumbnail'),
'menu_icon' => 'dashicons-megaphone',
/*'taxonomies' => array( 'category' )*/
)
);
}
add_action( 'init', 'codex_custom_init' );
步骤2:创建分类法
//taxonomy as joblocation
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
register_taxonomy(
'joblocation',
'jobs',
array(
'labels' => array(
'name' => 'Job Location',
'add_new_item' => 'Add New Job Location',
'new_item_name' => "New Job Location"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true
)
);
}
步骤3:page.php上的我的表单
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<span class="screen-reader-text"></span>
<input type="text" name="s" placeholder="Search Job Openings" id="search">
<input type="submit" id="searchsubmit" value="Search">
<input type="hidden" name="taxonomy" id="taxonomy" value="joblocation">
</form>
在这里阅读更多信息:什么时候应该使用WP\u Query vs Query\u posts()vs get\u posts()
你必须使用WP_查询来获取你需要的帖子。阅读相关文档。在您的情况下,查询可能如下所示:
$the_query = new WP_Query( array(
'post_type' => 'Jobs',
'tax_query' => array(
array (
'taxonomy' => 'joblocation',
'field' => 'slug',
'terms' => 'NewJersy',
)
),
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
// Show Posts ...
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
如果要按用户筛选,应将作业位置保存在用户元中,然后按如下所示进行税务查询:
$tax\u query=array(
'关系'=>'和',
排列(
“分类法”=>“作业位置”,
“术语”=>“1”,
'field'=>'term_id',
),
排列(
“分类法”=>“作业位置”,
“术语”=>“2”,
'field'=>'term_id',
),
);代码>因为我只有一个页面可以显示所有的工作地点作为taxonomy-joblocation.php,所以('terms'=>'NewJersy')将如何适用于所有地点??
<?php
$args = array(
'taxonomy' => 'joblocation',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
$the_query = new WP_Query( array(
'post_type' => 'Jobs',
'tax_query' => array(
array (
'taxonomy' => 'joblocation',
'field' => 'slug',
'terms' => 'NewJersy',
)
),
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
// Show Posts ...
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();