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

时间:2015-06-23 17:10:19

标签: php mysql wordpress

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

?>

1 个答案:

答案 0 :(得分:0)

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

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

切换&#34; post_type&#34;值为您的自定义帖子类型(不是&#34;帖子&#34;默认情况下是这样)。

例如:

&#39; post_type&#39; =&GT; &#39; customtype&#39;,