开发者

Cannot order posts by title

No matter how I try or what I do, I cannot order my posts by title:

# Vars
global $post;

# Build
$args = array(
    'numberposts'     => -1,
    'category'        => 28,
    'orderby'         => 'title',
    'order'           => 开发者_如何学JAVA'ASC',
    'post_type'       => 'post',
    'post_status'     => 'published' );

# Query
$tagposts = get_posts($args);

# Iterate
foreach($tagposts as $post) :

# Populate
setup_postdata($post);

# Show title, excerpt
echo '<tr><td>';
the_title();
echo '</td><td>';
the_excerpt();
echo '</td></tr>';

endforeach;

They always come out in the order I entered them. I cannot even get it to order by date.

EDIT: I am running 3.0.3 against MySQL on Windows

I should also note that the category I am searching on is a subcategory. The results show up fine.

Updated with latest technique which still results in the wrong order.


Try:

// display posts organized by title in ascending order

<div class="post">

    <h1>Ordered by Post Title (Ascending)</h1>

    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>

        <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

        <p><?php the_content(); ?></p>

    <?php endforeach; ?>

</div>

I have not used this Codex, but it looks like it may work here as well.

 <?php get_posts( $args ); ?> 

    'orderby'         => 'title',


I had this problem, and it was caused by the Post Types Order plugin. If you are using a plugin like this, it may override any custom sorting in your query.


This is really weird, but I solved the problem by adding a custom filter. Removing all of the current filters had no effect ... but this works:

# Vars
global $post;

# Build
$args = array(
    'numberposts'     => -1,
    'category'        => 28,
    'post_type'       => 'post',
    'post_status'     => 'published' );

# Clear
add_filter('posts_orderby', 'post_title ASC' );

# Query
$tagposts = get_posts($args);

# Iterate
foreach($tagposts as $post) :

# Populate
setup_postdata($post);

# Show title, excerpt
echo '<tr><td>';
the_title();
echo '</td><td>';
the_excerpt();
echo '</td></tr>';

endforeach;

# Order
function title_alphabetical( $orderby )
{
  return $orderby;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜