Sort news by date from several profiles

I have several profiles that allow users to post news that I want to display in one long feed, sorted in date order.

At the moment this loops through each profile, lists the news from that profile then moves onto the next profile, lists their news and so on. It doesn’t mix it all up.

Read More

How can I mix all the news together from each profile and order it by date?

<?php global $post;
$args = array('numberposts' => -1);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
   <?php while(the_repeater_field('news')): ?>
      <div class="social-news-item">
         <p class="social-news"><?php the_sub_field('news_description'); ?></p>
     <p class="social-company"><?php echo the_title(); ?></p>
      </div>
   <?php endwhile; ?>
<? endforeach; wp_reset_postdata(); ?>

The date field is:

<?php the_sub_field('date'); ?>

Related posts

Leave a Reply

4 comments

  1. If you want to change the order programmatically, have a look at the various array sorting functions in PHP, especially

    • uasort()— Sort an array with a user-defined comparison function and maintain index association
    • uksort()— Sort an array by keys using a user-defined comparison function
    • usort()— Sort an array by values using a user-defined comparison function

    but for wordpress checkout this http://codex.wordpress.org/Template_Tags/get_posts

    Example in given link:

    Default:

    <?php 
       $args = array(
        'posts_per_page'  => 5,
        'numberposts'     => 5,
        'offset'          => 0,
        'category'        => '',
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'include'         => '',
        'exclude'         => '',
        'meta_key'        => '',
        'meta_value'      => '',
        'post_type'       => 'post',
        'post_mime_type'  => '',
        'post_parent'     => '',
        'post_status'     => 'publish',
        'suppress_filters' => true );
     ?>
    

    Use:

     <?php $posts_array = get_posts( $args ); ?> 
    

    may this help you.

  2. As I wrote in the comment, I don’t know wordpress and maybe something is wrong, but the idea might be ok.

    If it is possible, you can first get all posts together and then sort them.

    To group them use a temporary array in which you fetch objects. So don’t output your divs but “buffer” them in the array.

    For example, if the array name would be $posts you can do in your foreach loop:
    (I hope in the line with the_sub_field('news_description'); you missed echo, in that case it won’t work)

    $var = &$posts[]; // this creates a new item in array, and $var is this element
    $var->description = the_sub_field('news_description'); // *****
    $var->title = the_title();
    $var->date = ... // it is important, I don't know if you have it
    

    So at the end your array will have all the posts from all users (or profiles). Now sort itfor example by using the usort function. You will need to write own sorting function, in the manual it is “cmp”, so I use the same name:

    function cmp($a, $b){
      if($a->date > $b->date)
        return 1;
      elseif($a->date < $b->date)
        return -1;
      else
        return 0;
    }
    

    and call it

    usort($posts, 'cmp');
    

    Then you need to output the array in another foreach loop.

    But, as I said, I don’t know if wordpress allows this.

  3. I’m assuming you mean ‘Authors’ when you refer to ‘Profiles’.

    Once you have all posts in $myposts

    $myposts = get_posts( $args );
    

    you iterate through them all with

    foreach( $myposts as $key => $post )
    

    (notice I’ve added the $key)

    Now you can create you own array with the date as a key

    $my_array[$myposts[$key]->post_date][] = ...whatevever you want in the end
    

    Feel free to add whatever data you want to display in the end. Let’s just take the author and title of the posts as an example:

    $the_item[$myposts[$key]->post_author] = $myposts[$key]->post_title;
    $my_array[$myposts[$key]->post_date][] = $the_item;
    

    This will result in an array that looks like this:

    [2007-11-19 22:46:37] => Array
        (
            [0] => Array
                (
                    [3] => Title
                    [2] => Another title
                )
    
        )
    
    [2007-11-11 11:11:11] => Array
        (
            [0] => Array
                (
                    [3] => Yet another title
                    [2] => Foo
                )
    
            [1] => Array
                (
                    [3] => Bar
                    [2] => Yuck
                )
    

    All posts are now ordered by date with posts by different authors ‘mixed’. Feel free to use any of the sort functions (as mentioned above). You might want to add a little randomness by using shuffle()…
    In order to display the posts you would go the opposite way: Iterate through your created array and print the data you included (e.g. author, title, content, etc.).
    For reference, this is what the post object looks like:

    myposts:Array
    (
        [0] => stdClass Object
            (
                [ID] => 1455
                [post_author] => 3
                [post_date] => 2013-03-27 22:16:33
                [post_date_gmt] => 2013-03-27 22:16:33
                [post_content] => Content
                [post_title] => Title
                [post_excerpt] => 
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] => 
                [post_name] => title
                [to_ping] => 
                [pinged] => 
                [post_modified] => 2013-03-27 22:16:42
                [post_modified_gmt] => 2013-03-27 22:16:42
                [post_content_filtered] => 
                [post_parent] => 0
                [guid] => http://www.abc.com/wordpress/?p=1455
                [menu_order] => 0
                [post_type] => post
                [post_mime_type] => 
                [comment_count] => 0
                [member_access_visibility] => default
                [filter] => raw
            )
    

    Hope this helps you solve your problem!

    Cheers

    JD

  4. You only need the one get_posts() function call.

    Create a string of all user id’s that you want separated by strings.

    Now you can add that to your args along with your order:

    $args = array(
        'numberposts' => -1,
        'author' => '1,2,3,4,5',
        'orderby' => 'date' // date is the default anyway, so shouldn't matter
    );
    

    Then your get_posts() call will include all posts by those authors.

    HTH