How to get all post titles starting with numbers and symbols?

I want to get all posts which are starting with a number (0-9) or symbols, in a list. I managed to get it work for letters, but this one is harder for me. Can someone help me out please?

I tried adding this in my existing code, but it didn't work:

$this_char = strtoupper(mb_substr($post-post_title, 0, 1, 'UTF-8'));
if (strpos('0123456789',$this_char) !== false) $this_char = '0-9';
if ($this_char != $last_char) {

This is my code:

?php

      $first_char = 'A';

    $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,
      'orderby'= 'title',
      'order' = 'ASC'
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query-have_posts() ) {
     echo 'test test '. $first_char;

      while ($my_query-have_posts()): $my_query-the_post(); 

?     
        lih1a href="?php the_permalink() ?" rel="bookmark" title="Permanent Link to ?php the_title_attribute(); ?"?php the_title(); ?/a
        /h1        /li
        ?php

      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?
/ul
/div

Note: I want all of these symbols and numbers on 1 page.

Topic get-the-title title get-posts query-posts posts Wordpress

Category Web


Hook into the posts_where filter and add your condition to the query:

add_filter('posts_where', function($where){
  global $wpdb;
  return "{$where} AND {$wpdb->posts}.post_title REGEXP '^[0-9\$\@\!\?\%]'";
                                // add other symbols in the regex above ^
});

Then do your query:

$my_query = new WP_Query(...);

You may want to remove the filter after you get the results (make it a normal function so you can pass the name to remove_filter).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.