I have a simple loop but it returns it in any order. What I'm looking for is to alphabetize the loop. I've tried a few different things but Can't seem to get it to work.

 // Start the loop.
 while ( have_posts() ) : the_post();

    // Include the page content template.
    get_template_part( 'content', 'grid-projects' );
 // End the loop.

You have to alter the query and add the orderby argument specified as name. I would go for the pre_get_posts action (see documentation) with some conditional tags to select the query for the right page.

For example:

function change_posts_order($query) {
  if($query->is_home() && $query->is_main_query()) { // edit to match the desired page
     $query->set('orderby', 'name');
     $query->set('order', 'ASC');
add_action('pre_get_posts', 'change_posts_order');
  • Your answer is probably right, unfortunatly I'm unable to use your example with my code. I can see the structure, but being a newbie I'm unable to modify it to work. thanks though. – Oscar Arango Jul 16 '15 at 12:52
  • I'm going to need an example using my code with it, I learn by seeing a working example that I drop in. – Oscar Arango Jul 16 '15 at 12:53
  • The code from your OP can stay the same. You just have to add my code to functions.php and edit it so it matches the page where your posts should be alphabetical. – leendertvb Jul 16 '15 at 16:16

this is what worked for me. works a treat.

$child_pages = new WP_Query( array(
  'orderby'     => 'name',
  'order'       => 'ASC',
  'cat'         => '41'
) );
while ( $child_pages->have_posts() ) : $child_pages->the_post();
  get_template_part( 'content', 'grid-projects' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.