我在 WordPress 中自定义帖子类型(测验)和搜索有问题。自定义帖子类型未显示在我的搜索结果页面中。我的搜索结果中仅显示默认帖子内容。
以下是我使用的代码
functions.php 函数 create_posttype() {
register_post_type( 'compassquiz',
array(
'labels' => array(
'name' => __( 'Compass Quiz' ),
'singular_name' => __( 'Compass Quiz' )
),
'taxonomies' => array('post_tag'),
'public' => true,
'publicly_queryable' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'free-quiz-bank-exam'),
'query_var' => true,
'exclude_from_search' => false,
)
);}
add_action( 'init', 'create_posttype' );
Run Code Online (Sandbox Code Playgroud)
functions.php 中的附加代码
//Read Custom Post types in Search
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'compassquiz'));
};
return $query;
};
add_filter('pre_get_posts', 'filter_search');
Run Code Online (Sandbox Code Playgroud)
搜索.php …
我有一个名为“事件”的自定义帖子类型。所有这些事件页面都有一个ACF 日期字段来输入事件开始日期。在事件存档页面中,我想将这些事件分为两组。
所以即将推出的活动将是这样的:
<?php
$today = date('Ymd');
$args = array(
'post_type' => 'events',
'nopaging' => true,
'orderby' => 'meta_value_num',
'meta_key' => 'event_start_date', //ACF date field
);
$upcoming_events = new WP_Query( $args );
if ( $upcoming_events->have_posts() ) :
?>
<h2>Upcoming Events</h2>
<ul>
<?php while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post(); ?>
<li>
Title: <?php the_title(); ?><br>
Date: <?php echo get_field('event_start_date'); ?>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
我知道有 meta_query …