【问题标题】:wordpress query all post that start with numberwordpress 查询所有以数字开头的帖子
【发布时间】:2016-12-29 05:21:33
【问题描述】:

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

我正在使用此代码:

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 个帖子中不起作用,但是当我将其更改为显示例如以 a 开头的帖子时,它会显示所有帖子。

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

谢谢。

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    要仅获取以数字开头的记录,请使用 REGEXP 过滤它们

    SELECT      ID
    FROM        $wpdb->posts
    WHERE       $wpdb->posts.post_title REGEXP '^[0-9]+'
    ORDER BY    $wpdb->posts.post_title;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多