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.
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'); ?>
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 associationuksort()
â Sort an array by keys using a user-defined comparison functionusort()
â Sort an array by values using a user-defined comparison functionbut for wordpress checkout this http://codex.wordpress.org/Template_Tags/get_posts
Example in given link:
Default:
Use:
may this help you.
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
div
s but “buffer” them in the array.For example, if the array name would be
$posts
you can do in yourforeach
loop:(I hope in the line with
the_sub_field('news_description');
you missedecho
, in that case it won’t work)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:
and call it
Then you need to output the array in another
foreach
loop.But, as I said, I don’t know if wordpress allows this.
I’m assuming you mean ‘Authors’ when you refer to ‘Profiles’.
Once you have all posts in $myposts
you iterate through them all with
(notice I’ve added the $key)
Now you can create you own array with the date as a key
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:
This will result in an array that looks like this:
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:
Hope this helps you solve your problem!
Cheers
JD
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:
Then your
get_posts()
call will include all posts by those authors.HTH