Order by post date with array_merge()? (3 posts)

  1. Code Junkie
    Posted 5 years ago #


    I'm working on a website where pages are listed out using get_pages in a gallery on the home page. Recently we added pages from a section section using two get_pages() calls and one array_merge() to get the array that would be used in the pages gallery list. Unfortunately, even though both arrays are set to sort by sort_column = post_date, one array is putting all of it's entries above the other array even if the dates of those posts are further back than some of the posts in the other array. Is there a way to merge the arrays and sort them in a way that maintains an order based on the sort dates of both arrays?

    You can see the code I'm using right now here:

    $videoPages = get_pages("child_of=$videos&number=$pageCount&offset=$startCount&sort_column=post_date&sort_order=DESC&parent=$videos");
                  $projectPages = get_pages("child_of=$portfolio&number=$pageCount&offset=$startCount&sort_column=post_date&sort_order=DESC&parent=$portfolio");
                  $pages = array_merge( $videoPages, $projectPages );

    A lot of the values in those arrays are set elsewhere.

    You can see the page I'm working on here: http://www.anthembranding.com

    Any help would be appreciated! Thanks.

  2. ambrosite
    Posted 5 years ago #

    Since get_pages returns an array of objects, you would have to use usort to sort the final merged array. Something along these lines:

    <?php usort( $pages, create_function('$a,$b', 'return strcmp($a->post_date, $b->post_date);') ); ?>

  3. Code Junkie
    Posted 5 years ago #

    Thank you for the help! That was just what I needed. As a note to anyone of my skill level with PHP who finds this and runs into the same problem I did, I was able to reverse the post order by changing ambrosite's code to:

    <?php usort( $pages, create_function('$a,$b', 'return strcmp($b->post_date, $a->post_date);') ); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic