Solved

Wordpress php code help needed

Posted on 2011-09-02
8
311 Views
Last Modified: 2012-08-14
I need to set up a page showing a list of authors who have posted within the last 30 days, and a list of their three most recent posts.

I found this code (below) which outputs a complete list of authors and their most recent 3 posts -  but it shows all authors, even ones that have not posted in a year or so.

Would you be able to adjust this code so it only shows authors who have posted within the last 30 days?

Thanks for any help,

Chris
<?php
//displays users not in the $skip_users array with their avatar and 3 of their posts (titles)
$skip_users = array(1,3);  //skip user ID 1 and 3
$blogusers = get_users_of_blog();
if ($blogusers) {
  foreach ($blogusers as $bloguser) {
    if (!in_array($bloguser->user_id, $skip_users) ) {
      $user = get_userdata($bloguser->user_id);
      echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
      echo get_avatar( $user->ID, 46 );
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        //echo 'List of Posts for ' . user->user_firstname . ' ' . $user->user_lastname;
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
    }
  wp_reset_query();  // Restore global post data stomped by the_post().
  }
}
?>

Open in new window

0
Comment
Question by:St_Aug_Beach_Bum
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:PragmatiCoder
ID: 36476897
Add a post_where filter to your query. Take a look at the "Return posts from the last 30 days"

http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36477427
To show authors that have posted within the last 30 days would require a three step approach (BTW, the function get_users_of_blog() has been deprecated and should no longer be used):
<?php

// First, create a new filtering function that limits posts to last 30 days
function filter_where( $where = '' ) {
	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}

add_filter( 'posts_where', 'filter_where' );

// Next, find authors with posts within last 30 days
$args = array(
     'blog_id' => $GLOBALS['blog_id'],
     'who' => 'author',
 );
$skip_users = array(1,3);  //skip user ID 1 and 3
$blogusers = get_users($args);
$authors = array();
if ($blogusers) {
  $i = 0;
  foreach ($blogusers as $bloguser) {
    if (!in_array($bloguser->user_id, $skip_users) ) {
      $user = get_userdata($bloguser->user_id);
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
      );
      $query = new WP_Query($args);
      if( $query->have_posts() ) {
        //echo 'List of Posts for ' . $user->user_firstname . ' ' . $user->user_lastname;
        while ($query->have_posts()) : $query->the_post();
            $authors[$i] = $user->ID;
            $i++;
            break;
        endwhile;
      }
    }
  wp_reset_query();  // Restore global post data stomped by the_post().
}
remove_filter( 'posts_where', 'filter_where' );
 
// Finally, print out the last three posts of each of those authors
if (count($authors) > 0) {
  foreach ($authors as $author) {
      $user = get_userdata($author);
      echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
      echo get_avatar( $user->ID, 46 );
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        //echo 'List of Posts for ' . user->user_firstname . ' ' . $user->user_lastname;
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
    }
  wp_reset_query();  // Restore global post data stomped by the_post().
  }
}
?>

Open in new window

0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36477433
Sorry, I forgot to correct your code on line 59.  user->user_firstname should be $user->user_firstname.
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36477439
I found more mistakes.  Try this one:

<?php

// First, create a new filtering function that limits posts to last 30 days
function filter_where( $where = '' ) {
	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}

add_filter( 'posts_where', 'filter_where' );

// Next, find authors with posts within last 30 days
$args = array(
     'blog_id' => $GLOBALS['blog_id'],
     'who' => 'author'
 );
$skip_users = array(1,3);  //skip user ID 1 and 3
$blogusers = get_users($args);
$authors = array();
if ($blogusers) {
  $i = 0;
  foreach ($blogusers as $bloguser) {
    if (!in_array($bloguser->user_id, $skip_users) ) {
      $user = get_userdata($bloguser->user_id);
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
      );
      $query = new WP_Query($args);
      if( $query->have_posts() ) {
        while ($query->have_posts()) : $query->the_post();
            $authors[$i] = $user->ID;
            $i++;
            break;
        endwhile;
      }
    }
  wp_reset_query();  // Restore global post data stomped by the_post().
}
remove_filter( 'posts_where', 'filter_where' );
 
// Finally, print out the last three posts of each of those authors
if (count($authors) > 0) {
  foreach ($authors as $author) {
      $user = get_userdata($author);
      echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
      echo get_avatar( $user->ID, 46 );
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        //echo 'List of Posts for ' . $user->user_firstname . ' ' . $user->user_lastname;
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
    }
  wp_reset_query();  // Restore global post data stomped by the_post().
  }
}
?>

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:St_Aug_Beach_Bum
ID: 36478401
Thank you very much for your help with this.

With the script above, I get 'user id' (not the id, just the words) and the same three links over and over again.

I don't really need the user id displayed, just the name and then the three links.

Thanks again, I'll keep playing with it on this end, but my php skills are rather limited,

Chris
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 36478408
I am using the latest wordpress version, if that makes a difference here.
0
 
LVL 5

Accepted Solution

by:
PragmatiCoder earned 500 total points
ID: 36478831
Tested and working
<?php
//displays users not in the $skip_users array with their avatar and 3 of their posts (titles)
function filter_where( $where = '' ) {
	// posts in the last 30 days
	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}
$skip_users = array(1,3);  //skip user ID 1 and 3
$blogusers = get_users_of_blog();
if ($blogusers) {//var_dump($blogusers);
  foreach ($blogusers as $bloguser) {
    //if (!in_array($bloguser->user_id, $skip_users) ) {
      $user = get_userdata($bloguser->user_id);
      echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . '</p>';
      echo get_avatar( $user->ID, 1 );
      $args=array(
        'author' => $user->ID,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 3,
        'caller_get_posts'=> 1
      );
      $my_query = null;

      add_filter( 'posts_where', 'filter_where' );
      $my_query = new WP_Query($args);
      remove_filter( 'posts_where', 'filter_where' );
      if( $my_query->have_posts() ) {
        //echo 'List of Posts for ' . user->user_firstname . ' ' . $user->user_lastname;
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
    //}
  wp_reset_query();  // Restore global post data stomped by the_post().
  }
}
?>

Open in new window

0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 36478929
Awesome  :)

Thank you very much!

Chris
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Do you think that WordPress is just for blogs?  Think again!  WordPress is really a fantastic all around platform that you can use to develop websites on.  Integrated into its basic functionality is the ability to create pages using your choice of a…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The viewer will learn how to dynamically set the form action using jQuery.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now