Solved

wordpress archive sort alphabetically

Posted on 2014-10-13
2
274 Views
Last Modified: 2014-10-13
How can I alphabetically sort the posts by title before displaying each post?

Here's the code that I'm using to display the posts in an archive page.
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();

$queried_post = get_post($post_id);
$title = $queried_post->post_title;

echo '<p>'.$queried_post->post_content.'</p>';

endwhile;
endif;
?>

Open in new window


FYI:  I am handling the query in this manner because I need to grab the 'raw' contents of the post (including the HTML tags) - not the usual "display a WP post."  However, if anyone has an improved manner for 'grabbing' the post content, that's fine too.  Basically, any help is appreciated.
0
Comment
Question by:mar2195
  • 2
2 Comments
 

Accepted Solution

by:
mar2195 earned 0 total points
ID: 40379116
I found the answer!

Posting here for others.

(*This goes into your theme's " function.php " file)

<?php
add_filter("posts_orderby", "my_orderby_filter", 10, 2);
function my_orderby_filter($orderby, &$query){
    global $wpdb;
    if (get_query_var("post_type") == "your-post-type") {
         return "$wpdb->posts.post_title ASC";
    }
    return $orderby;
}
?>

Open in new window

0
 

Author Closing Comment

by:mar2195
ID: 40379118
Although this seems to be an easy solution and your would think, an easy to solution find, the answer to this was actually difficult to find.  So I hope this posting helps others.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since I am currently running into this issue right now as I type this, I though I would share my experiences with moving a ModX Revolution site from one server to another.  It is not an easy task, but can be accomplished rather easily. The first …
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now