Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Wordpress php code help needed

Posted on 2011-09-02
8
Medium Priority
?
330 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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