• I’m having problems trying to do a simple sorting task. I’ve entered the code below in category.php”

    $queryP = new WP_Query( array ( 'orderby' => 'title', 'order' => 'DESC' ) );
    #$queryP->query();
    
    while ( $queryP->have_posts() ) : $queryP->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;

    Titles are not sorted the way I had hoped it would. What did I miss out? I’m running WordPress 3.2

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Remove #$queryP->query();

    If it is the categories page then you do not need WP_Query() as query_posts($args); can re-sort the existing post set, if I am right without re-doing the query.

    <?php
    $query='orderby=title&order=DESC';
    query_posts($query);
    ?>
    
    <?php if ( have_posts() ) : ?>

    HTH

    David

    Thread Starter jundolor

    (@jundolor)

    Thanks, but the sorting still does not work. I’ve rewritten the code as follows:

    <?php
    $query='cat=3&orderby=title&order=DESC';
    query_posts($query);
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>

    The query gets to select the posts in the category, but still the sorting does not work.

    Ok,
    I have modified one of my downloadable twenty eleven child themes which outputs a page of posts, it uses custom fields and there is a download in the link.

    Then grab this code from pastebin and replace all the code in the download file page-category.php

    The additional Custom Field is:
    ordered
    The options: (Default ID)
    ‘none’,
    ‘ID’,
    ‘author’,
    ‘title’ ,
    ‘date’ ,
    ‘modified’,
    ‘parent’,
    ‘rand’,
    ‘comment_count’,
    ‘menu_order’

    I tested this with custom fields:

    category = blog (one of my categories)
    asc = true
    ordered = title

    category = blog (one of my categories)
    ordered = title

    category = blog (one of my categories)
    ordered = rand

    All worked as expected!

    HTH

    David

    I extended my page of posts to use meta data, you might find the code in the page template handy.

    Regards

    David

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WP_Query sorting does not work’ is closed to new replies.