0

I have problem with sort posts according to date:

My code now:

<?php
global $switched;
$original_blog_id = get_current_blog_id(); // get current blog

$blog_ids = array(4,1);

foreach( $blog_ids as $blog_id ){
switch_to_blog( $blog_id );
$args = array( 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ):
setup_postdata( $post ); ?>
<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php  endforeach; 
wp_reset_postdata();

switch_to_blog( $original_blog_id ); //switched back to current blog

restore_current_blog();
}
?>

My problem:

http://images.tinypic.pl/i/00700/vracxlcdm296.png

I would like:

http://images.tinypic.pl/i/00700/jo2yp8f83bce.png

I want to sort by date of all blogs. Not everyone separately.

3
  • 2
    And that problem is what...?
    – vancoder
    Sep 8 '15 at 23:50
  • What is exactly not happening or happening that should not. Please be specific, as your question stand you will in all propability not get answer :-) Sep 9 '15 at 4:52
  • You have another problem in your code. When you use switch_to_blog() you need to use restore_current_blog() afterward; if you don't, things get messy. See this answer and this Q&A for details.
    – Pat J
    Sep 10 '15 at 14:00
1

Here's how I'd attack your problem (in fact I think I've written code like this in the past, but I can't find it at the moment):

$blogs = array( 4, 1 );
$all_posts = array();
foreach( $blogs as $blog ) {
    switch_to_blog( $blog );
    $args = array( 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC');
    $blog_posts = get_posts( $args );
    foreach( $blog_posts as $blog_post ) {
        // Add the correct permalink to the Post object
        $blog_post->permalink = get_the_permalink( $blog_post->ID );
        $all_posts[] = $blog_post;
    }
    restore_current_blog();
}

usort( $all_posts, '__sort_by_date' );

// Now you can display all your posts

foreach( $all_posts as $post ) {
    setup_postdata( $post );
    the_title( '<a href="' . $post->permalink . '">', '</a>' );
}


function __sort_by_date( $a, $b ) {
    $a_date = strtotime( $a->post_date );
    $b_date = strtotime( $b->post_date );
    if( $a_date === $b_date ) { return 0; }
    if( $a_date > $b_date ) { return 1; }
    return -1;
}

Note: I've updated the code to get the correct permalink for each post while we're in the proper site (per your comment/answer below).

References

6
  • 1
    You can actually combine the last two lines with the ternary operator (?). It would look like: return ($a_date > $b_date) ? 1 : -1;
    – Cedon
    Sep 10 '15 at 15:19
  • Indeed. I try to keep away from things like ? if I'm going to be passing my code to (for example) my co-workers, since they're less versed in PHP.
    – Pat J
    Sep 10 '15 at 15:23
  • Thank you very much for your answer. All working, but it doesn't working get_the_permalink(). Problem: When code is on blog "blog_1", link on to my posts on other blog(name blog: blog_3) is "www.name.com/blog_1/2015/09/08/dzfsdsa/" instead "www.name.com/blog_3/2015/09/08/dzfsdsa/" I change get_the_permalink() on get_the_guid() and working, but i don't know if I'm doing it well. z How safely add blogs in $blogs = array( 4, 22 ); by sql?
    – cfnne
    Sep 11 '15 at 21:36
  • 1) Are you getting an error message from get_the_permalink() in the code? 2) You should be able to simply add blog IDs to the $blogs array (eg: $blogs = array( 4, 22, 19, 27, 1, 42, ); .
    – Pat J
    Sep 12 '15 at 15:14
  • 1. I don't have messages in code about error. Error is only in address for posts. Function, add adress /2015/09/08/name_post/ If i am on website about adress www.name.com/blog1 I getting adress www.name.com/blog1/2015/09/08/name_post/ which don't working, because working adress it www.name.com/blog3/2015/09/08/name_post/ . In short: Correctly reads adress for post, but doesn't blog on which this post is located.
    – cfnne
    Sep 14 '15 at 10:41
0

You can add argument as below for get_posts method

'orderby'          => 'date',
'order'            => 'DESC',

For more help please visite below link https://codex.wordpress.org/Template_Tags/get_posts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.