Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pgroot91/1e241aa3de3053218cd2b3245ede7036 to your computer and use it in GitHub Desktop.
Save pgroot91/1e241aa3de3053218cd2b3245ede7036 to your computer and use it in GitHub Desktop.

Revisions

  1. @urre urre revised this gist Dec 11, 2014. No changes.
  2. @urre urre revised this gist Dec 11, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -68,7 +68,7 @@ function sort_objects_by_date($a, $b) {
    $i++;
    endforeach;

    # Our main loop now sorted and unique authors. Example.
    # Our main loop now sorted and unique authors.
    global $post;
    foreach( $posts as $post ) :

    @@ -89,11 +89,11 @@ function sort_objects_by_date($a, $b) {
    $author_id = get_the_author_meta( 'ID' );
    $author_show = get_the_author_meta( 'use' );
    $author_guest = get_the_author_meta( 'guest' );
    $author_color = get_the_author_meta('color'); // Just an example of a custom field add to a users profilepage
    $author_color = get_the_author_meta('color'); // Just an example of a custom field added to a users profilepage
    endif;
    endforeach;

    # Do something wihth it now here! ...
    # Do something wihth the data now here! ...

    endforeach;
    wp_reset_postdata();
  3. @urre urre revised this gist Dec 11, 2014. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -68,23 +68,28 @@ function sort_objects_by_date($a, $b) {
    $i++;
    endforeach;

    # Our main loop now sorted and unique authors
    # Our main loop now sorted and unique authors. Example.
    global $post;
    foreach( $posts as $post ) :

    $post_title = get_the_title();
    $post_date = get_the_date();
    $author_meta = get_the_author_meta('choosecolor');
    $author_meta = get_the_author_meta('color'); // Just an example of a custom field added to the users profile page

    # Get meta data depending on context i.e use switch_to_blog()
    foreach( $blog_ids as $blog ) :
    switch_to_blog($blog);
    setup_postdata($post);
    if(get_post_thumbnail_id($id)):
    $featured_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'featured_post_image' )[0];
    $post_link = get_permalink();
    $blog_link = get_bloginfo('url');
    $avatar = get_wp_user_avatar(get_the_author_meta('ID'), 42);
    $attachment_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'featured_post_image' );
    $attachment_image_url = $attachment_image[0];
    $postlink = get_permalink();
    $bloglink = get_bloginfo('url');
    $avatar = get_wp_user_avatar(get_the_author_meta('ID'), 42);
    $author_id = get_the_author_meta( 'ID' );
    $author_show = get_the_author_meta( 'use' );
    $author_guest = get_the_author_meta( 'guest' );
    $author_color = get_the_author_meta('color'); // Just an example of a custom field add to a users profilepage
    endif;
    endforeach;

  4. @urre urre revised this gist Dec 11, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -87,6 +87,8 @@ function sort_objects_by_date($a, $b) {
    $avatar = get_wp_user_avatar(get_the_author_meta('ID'), 42);
    endif;
    endforeach;

    # Do something wihth it now here! ...

    endforeach;
    wp_reset_postdata();
  5. @urre urre revised this gist Dec 11, 2014. 1 changed file with 32 additions and 12 deletions.
    44 changes: 32 additions & 12 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -11,21 +11,20 @@
    AND blog_id != 1
    ");


    // New empty arrays
    $blog_ids;
    $blogusers;
    $blogusers_ids;

    // Only save blog id numbers into the new array, also save all blogusers in network
    foreach ( $blogs as $bloggers ) {
    $blog_ids[] = $bloggers->blog_id;
    $blogusers[] = get_users( 'blog_id='.$bloggers->blog_id.'');
    $blog_ids[] = $bloggers->blog_id;
    $blogusers[] = get_users( 'blog_id='.$bloggers->blog_id.'');
    }

    // Save blog user ids in network
    foreach ( $blogusers as $user ) {
    $blogusers_ids[] = $user->user_id;
    $blogusers_ids[] = $user->user_id;
    }

    // Save latest post from every blog, ordered by date. Add to a array.
    @@ -34,10 +33,10 @@
    switch_to_blog( $blog_id );
    $query = new WP_Query(
    array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'orderby' => 'date',
    'order' => 'DESC'
    'post_type' => 'post',
    'posts_per_page' => 1,
    'orderby' => 'date',
    'order' => 'DESC'
    )
    );
    while ( $query->have_posts() ) {
    @@ -49,8 +48,8 @@

    // Compare dates ASC
    function sort_objects_by_date($a, $b) {
    if($a->post_date == $b->post_date){ return 0 ; }
    return ($a->post_date > $b->post_date) ? -1 : 1;
    if($a->post_date == $b->post_date){ return 0 ; }
    return ($a->post_date > $b->post_date) ? -1 : 1;
    }

    // Sort array of posts by date
    @@ -64,9 +63,30 @@ function sort_objects_by_date($a, $b) {
    $got[] = $d->post_author;
    $out[] = $d;
    else:
    unset($posts[$i]);
    unset($posts[$i]);
    endif;
    $i++;
    endforeach;

    ?>
    # Our main loop now sorted and unique authors
    global $post;
    foreach( $posts as $post ) :

    $post_title = get_the_title();
    $post_date = get_the_date();
    $author_meta = get_the_author_meta('choosecolor');

    # Get meta data depending on context i.e use switch_to_blog()
    foreach( $blog_ids as $blog ) :
    switch_to_blog($blog);
    setup_postdata($post);
    if(get_post_thumbnail_id($id)):
    $featured_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'featured_post_image' )[0];
    $post_link = get_permalink();
    $blog_link = get_bloginfo('url');
    $avatar = get_wp_user_avatar(get_the_author_meta('ID'), 42);
    endif;
    endforeach;

    endforeach;
    wp_reset_postdata();
  6. @urre urre revised this gist Dec 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@

    // Save blog user ids in network
    foreach ( $blogusers as $user ) {
    $blogusers_ids[] = $user->user_email;
    $blogusers_ids[] = $user->user_id;
    }

    // Save latest post from every blog, ordered by date. Add to a array.
  7. @urre urre revised this gist Dec 5, 2014. 1 changed file with 30 additions and 11 deletions.
    41 changes: 30 additions & 11 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,21 @@
    AND blog_id != 1
    ");

    // New empty array

    // New empty arrays
    $blog_ids;
    $blogusers;
    $blogusers_ids;

    // Only save blog id numbers into the new array
    // Only save blog id numbers into the new array, also save all blogusers in network
    foreach ( $blogs as $bloggers ) {
    $blog_ids[] = $bloggers->blog_id;
    $blogusers[] = get_users( 'blog_id='.$bloggers->blog_id.'');
    }

    // Save blog user ids in network
    foreach ( $blogusers as $user ) {
    $blogusers_ids[] = $user->user_email;
    }

    // Save latest post from every blog, ordered by date. Add to a array.
    @@ -38,16 +47,26 @@
    restore_current_blog();
    }

    // Compare dates ASC
    function sort_objects_by_date($a, $b) {
    if($a->post_date == $b->post_date){ return 0 ; }
    return ($a->post_date > $b->post_date) ? -1 : 1;
    }

    // $posts now has all the posts i want, but not ordered correctly.
    // Sort array of posts by date
    usort($posts, 'sort_objects_by_date');

    // Remove duplicate authors, only display 1 posts from 1 author
    $got = array();
    $i = 0;
    foreach($posts as $i => $d):
    if(!in_array($d->post_author, $got)):
    $got[] = $d->post_author;
    $out[] = $d;
    else:
    unset($posts[$i]);
    endif;
    $i++;
    endforeach;

    // This loops out blog posts from all blogs but starting from blog id 2,3,4 osv.
    // TODO: order whole array by date
    // TODO: exclude if post author apperar more than once
    foreach ( $posts as $postss ) {
    echo $postss->post_date;
    echo $postss->post_title;
    echo '<br><br>';
    }
    ?>
  8. @urre urre revised this gist Dec 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@
    // $posts now has all the posts i want, but not ordered correctly.


    // This loops out blog posts from all blogs but starting from blog id 1,2,4 osv.
    // This loops out blog posts from all blogs but starting from blog id 2,3,4 osv.
    // TODO: order whole array by date
    // TODO: exclude if post author apperar more than once
    foreach ( $posts as $postss ) {
  9. @urre urre renamed this gist Dec 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. @urre urre created this gist Dec 5, 2014.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?php

    // Get all blog ids in network except main site (id 1)
    $blogs = $wpdb->get_results("
    SELECT blog_id
    FROM {$wpdb->blogs}
    WHERE site_id = '{$wpdb->siteid}'
    AND spam = '0'
    AND deleted = '0'
    AND archived = '0'
    AND blog_id != 1
    ");

    // New empty array
    $blog_ids;

    // Only save blog id numbers into the new array
    foreach ( $blogs as $bloggers ) {
    $blog_ids[] = $bloggers->blog_id;
    }

    // Save latest post from every blog, ordered by date. Add to a array.
    $posts = array();
    foreach ( $blog_ids as $blog_id ) {
    switch_to_blog( $blog_id );
    $query = new WP_Query(
    array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'orderby' => 'date',
    'order' => 'DESC'
    )
    );
    while ( $query->have_posts() ) {
    $query->next_post();
    $posts[] = $query->post;
    }
    restore_current_blog();
    }


    // $posts now has all the posts i want, but not ordered correctly.


    // This loops out blog posts from all blogs but starting from blog id 1,2,4 osv.
    // TODO: order whole array by date
    // TODO: exclude if post author apperar more than once
    foreach ( $posts as $postss ) {
    echo $postss->post_date;
    echo $postss->post_title;
    echo '<br><br>';
    }
    ?>