Forums

WP_Query sorting does not work (6 posts)

  1. jundolor
    Member
    Posted 7 months ago #

    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

  2. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    Remove #$queryP->query();

  3. Digital Raindrops
    Member
    Posted 7 months ago #

    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

  4. jundolor
    Member
    Posted 7 months ago #

    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.

  5. Digital Raindrops
    Member
    Posted 7 months ago #

    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

  6. Digital Raindrops
    Member
    Posted 7 months ago #

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

    Regards

    David

Reply

You must log in to post.

About this Topic

Tags