Custom Feed Orderby has no effect (3 posts)

  1. antonyjosephsmith
    Posted 2 years ago #

    Hi all,

    I'm trying to create a custom feed which grabs the days posts and then puts them all in alphabetical order. I can get them in alphabetical order or I can get all the days posts. However once I filter by date and then try to order them by title nothing happens.

    The code I'm using is here:

    $current_date = getdate();
    $args = array(
    	'posts_per_page'   	=> 90,
    	'year'     			=> $current_date["year"],
    	'monthnum' 		=> $current_date["mon"],
    	'day'				=> $current_date["mday"],
    	'orderby' 			=> 'title',
    	'order'    			=> 'ASC'
    query_posts( $args );

    You can see a sample of the feed here:


    Any help would be much appreciated.


  2. Antoine K
    Posted 2 years ago #

    Hi I am having the same difficulties, have you found a way to fix this ?


  3. antonyjosephsmith
    Posted 2 years ago #

    I'm aware of a solution but haven't had chance to try fix it yet. Are you any good with PHP? The answer lies in here:

    Having looked at the documentation and source code for the query_posts() function, it uses the $GLOBALS super-global array, so unless you call wp_reset_query() afterwards, further queries may not behave as expected.

    Since the function returns an array of posts, my suggestion would be to use the PHP internal function usort() (http://uk3.php.net/usort). This allows you to sort a multi-dimensional array by user-defined criteria. In this case I would suggest pulling out the days' posts using query_posts (or get_posts(), which has less side effects), then usort() the resulting array to alphabetise the array on the title array element.

    The PHP manual gives a simple example fo how this could work:


    function compare($a, $b) {
    return strcmp($a["fruit"], $b["fruit"]);

    $fruits[0]["fruit"] = "lemons";
    $fruits[1]["fruit"] = "apples";
    $fruits[2]["fruit"] = "grapes";

    usort($fruits, "cmp");

    Which, if you output the contents of the $fruits array, gives you:


Topic Closed

This topic has been closed to new replies.

About this Topic