Sort news by date from multiple profiles

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

At the moment, it goes through each profile, lists the news from this profile, then goes to the next profile, lists their news, etc. He does not mix it all.

How to combine all the news 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(); ?> 

Date Field:

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

+4
source share
4 answers

If you want to programmatically reorder, look at the various array sorting functions in PHP , especially

  • uasort() - sort an array with a custom comparison function and maintain index association
  • uksort() - sort an array using a custom comparison function
  • usort() - Sort an array by values ​​using a custom comparison function

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

An example in this link:

The default value is:

 <?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 ); ?> 

Application:

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

can it help you.

+3
source

As I wrote in a comment, I don’t know wordpress and maybe something is wrong, but the idea may be fine.

If possible, you can collect all the messages first and then sort them.

To group them, use a temporary array in which you retrieve the objects. Therefore, do not output the div into the array, but "buffer" in the array.

For example, if the array name is $posts , you can do in your foreach : (I hope that in the line with the_sub_field('news_description'); you skipped echo , in this case it will not 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 messages from all users (or profiles). Now collect it, for example, using the usort function . You will need to write your own sort 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 name him

 usort($posts, 'cmp'); 

Then you need to output the array to another foreach .

But, as I said, I do not know if Wordpress allows this.

+2
source

I assume you mean Authors when you refer to Profiles.

Once you have all the posts in $ myposts

 $myposts = get_posts( $args ); 

you repeat all of them with

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

(note that I added the $ key)

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

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

Remember to add all the data you want to display at 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 messages are now ordered by date with messages from different authors. Feel free to use any of the sorting functions (as above). You might want to add a bit of randomness using shuffle () ... To display messages, you have to go the other way around: Iterate through your created array and print out the data that 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!

Greetings

Jd

+1
source

You only need one call to get_posts() .

Create a row of the entire user ID that you want to split line by line.

Now you can add this to your arguments 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 call to get_posts() will contain all the posts of these authors.

NTN

0
source

All Articles