如何获取所有以数字和符号开头的帖子标题?

时间:2013-06-16 作者:user1627363

我想获得列表中所有以数字(0-9)或符号开头的帖子。我设法让它为信件工作,但这个对我来说更难。有人能帮帮我吗?

我尝试将此添加到现有代码中,但没有成功:

$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) {
这是我的代码:

<?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>
注意:我希望所有这些符号和数字都在一页上。

1 个回复
SO网友:onetrickpony

钩入posts_where 筛选条件并将其添加到查询中:

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 ^
});
然后执行查询:

$my_query = new WP_Query(...);
您可能希望在获得结果后删除过滤器(使其成为普通函数,以便将名称传递给remove_filter).

结束

相关推荐

Add title to image post

在最近的帖子中,我无法在特色图片上添加标题。尝试此操作,但仅获取alt<?php $default_attr = array( \'src\' => $src, \'class\' => \"attachment-$size\", \'alt\' => trim(strip_tags( $attachment->post_excerpt )), \'title\' =>