Solved

Wordpress php code help needed

Posted on 2011-09-02
8
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

761 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