最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query posts - query_posts doesn't order by title

programmeradmin2浏览0评论

It seems like stupid bug, but how can i order by title? It anyways orders by date! I'm using:

query_posts( array( 
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'orderby'        => 'title',
    'order'          => 'ASC'
) );

I want to use this in function as SHORTCODE. What i'm trying to achieve is to create site map / index of all entries in alphabetic order. Also i'm using newest WP.

It seems like stupid bug, but how can i order by title? It anyways orders by date! I'm using:

query_posts( array( 
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'orderby'        => 'title',
    'order'          => 'ASC'
) );

I want to use this in function as SHORTCODE. What i'm trying to achieve is to create site map / index of all entries in alphabetic order. Also i'm using newest WP.

Share Improve this question edited Jun 6, 2012 at 17:24 Tommixoft asked Jun 6, 2012 at 9:42 TommixoftTommixoft 7501 gold badge5 silver badges15 bronze badges 7
  • Can we see a var_dump() or print_r() of the generated query? Also, a bit of context for this code would be helpful. Are you modifying the main loop query, or running a secondary loop? – Chip Bennett Commented Jun 6, 2012 at 15:26
  • i created function which works as shortcode, it displays all entries like Index, let's say it shows letter and after letter all entries who starts with that leter, like site map. I'll post you array in Question. – Tommixoft Commented Jun 6, 2012 at 16:36
  • Well array looked HUGE and it was ordered by date, so i decided not to post it here. I'm not new to php or wordpress so i don't think that i'm missing somehing in code. I posted you simple version of my function. – Tommixoft Commented Jun 6, 2012 at 16:45
  • 2 You're using this as a SHORTCODE? Meaning you're executing it inside the post content? – Chip Bennett Commented Jun 6, 2012 at 16:51
  • Yes, but i'm using shortcode in page, also my content is empty, only shortcode, anyway, so what can i do? should i make template and avoid shortcode? Also i think it should reset the query and make new one with mine..so where the problem? i'm confused – Tommixoft Commented Jun 6, 2012 at 16:53
 |  Show 2 more comments

2 Answers 2

Reset to default 10

Thanks to Chip Bennett who told me that i'm doing wrong by using query_posts inside content. So i used get_posts and i got what i wanted, thanks!

Here is sample of how can you do it, if you got the same problem as me:

function some_name(){

global $post;
$tmp_post = $post;
$args = array( 'post_type'=>'page', 'numberposts' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$myposts = get_posts( $args );

         if ( !empty($myposts) ) {
              foreach( $myposts as $post ) : setup_postdata($post);
              the_title();
              echo '<br>';             

              endforeach;

         } 

          $post = $tmp_post;

 }

Another post has this code inside a function, but for those looking to simply add alphabetized posts to a custom theme, for example, you can use the following...

<?php // Alphabetize posts from the *** category
global $post;
$temp_post = $post;
$args = array( 'category' => *categoryID*, 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' );
$these_posts = get_posts( $args );

if( !empty($these_posts) ) {
    foreach( $these_posts as $post ) : setup_postdata($post);
?>

<?php 

/* HTML/PHP code goes here to display each post */

the_title("<h2>", "</h2>");
the_content("<div class='content'>", "</div>"); 

?>

<?php 
    endforeach; // End of The Loop.
}
$post = $temp_post;
?>
发布评论

评论列表(0)

  1. 暂无评论