Solved

wordpress archive sort alphabetically

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Sessions in WordPress 8 30
php string detection problem 7 33
Which userID to use for shopping cart application 13 19
Decrypt string by php 7 29
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…
The viewer will learn how to dynamically set the form action using jQuery.

785 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