Solved

Wordpress php code help needed

Posted on 2011-09-02
8
328 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

624 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