wordpress查询所有以数字开头的帖子

时间:2016-12-29 05:21:34

标签: php mysql wordpress

我正在尝试显示包含数字的所有帖子。

我正在使用此代码:

global $wpdb;
$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",REGEXP ^[0-9])); 
if ($postids) {
$args=array(
  'post__in' => $postids,
  'post_type' => 'shows',
  '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 '. $_GET['letter'];
  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();
}

我已经尝试了这个,但它只是在1个帖子上工作,但当我更改它以显示例如以它开头的帖子显示所有这些。

  RLIKE   ^[0-9] [0-9]%        

感谢。

1 个答案:

答案 0 :(得分:0)

要仅提取以数字开头的记录,请使用REGEXP

对其进行过滤
SELECT      ID
FROM        $wpdb->posts
WHERE       $wpdb->posts.post_title REGEXP '^[0-9]+'
ORDER BY    $wpdb->posts.post_title;