如何获取帖子以自定义帖子类型的给定字母开头?

问题描述 投票:0回答:1

我正在寻找一种解决方案来获取自定义帖子类型的所有帖子一段时间。我找到了一个有用的答案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().
}

?>
php mysql wordpress
1个回答
0
投票

您可以在以下位置选择帖子类型:

$args=array(
  'post__in' => $postids,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);

通过将“post_type”值切换为自定义帖子类型(默认情况下不是“post”)。

例如:

'post_type'=>'customtype',

© www.soinside.com 2019 - 2024. All rights reserved.