WordPress.org

Ready to get started?Download WordPress

Forums

Custom Feed Orderby has no effect (3 posts)

  1. antonyjosephsmith
    Member
    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:

    <?php
    $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 );
    include('wp-includes/feed-rss2.php');
    ?>

    You can see a sample of the feed here:

    feed://members.maxwelllucas.com/blog/?feed=gw-news

    Any help would be much appreciated.

    Thanks

  2. Antoine K
    Member
    Posted 1 year ago #

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

    Thanks.

  3. antonyjosephsmith
    Member
    Posted 1 year 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:

    <?php

    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:

    apples
    grapes
    lemons

Topic Closed

This topic has been closed to new replies.

About this Topic