我正在寻找一种解决方案来获取自定义帖子类型的所有帖子一段时间。我找到了一个有用的答案here 。但使用该代码片段的主要问题是,我想获取特定自定义帖子类型的帖子。普通帖子是博客帖子,所以我想过滤那些普通帖子。我不太擅长mysql,所以请求解决方案。
获取给定信件的所有帖子的片段:
<?php
//get all post IDs for posts beginning with cap B, in title order,
//display posts
$first_char = 'B';
$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $first_char;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
?>
最佳答案
您可以选择帖子类型:
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
将“post_type”值切换为您的自定义帖子类型(而不是默认的“post”)。
例如:
'post_type' => '自定义类型',
关于php - 如何获取以给定字母开头的自定义帖子类型的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31009178/