ANto1
ANto1

Reputation: 21

wordpress query all post that start with number

I am trying to show all posts that contain a number.

I am using this code :

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();
}

I have tried this but it is not working it just 1 post, but when i change it to show for example posts that start with a it show all of them.

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

Thanks.

Upvotes: 2

Views: 724

Answers (1)

Samir Selia
Samir Selia

Reputation: 7065

To fetch only records starting with number, filter them using REGEXP

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

Upvotes: 0

Related Questions