Solved

wordpress archive sort alphabetically

Posted on 2014-10-13
2
277 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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!
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 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