Solved

Wordpress php code help needed

Posted on 2011-09-02
8
316 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

770 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