Solved

wordpress archive sort alphabetically

Posted on 2014-10-13
2
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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