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

admin2025-01-07  7

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

?>     
        <li><h1><a 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.

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

?>     
        <li><h1><a 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.

Share Improve this question edited Jul 6, 2013 at 9:58 user1627363 asked Jun 16, 2013 at 15:24 user1627363user1627363 1724 silver badges21 bronze badges 1
  • No one who can help? – user1627363 Commented Jul 3, 2013 at 18:23
Add a comment  | 

1 Answer 1

Reset to default 0

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).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262177a798.html

最新回复(0)