Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to write a function in functions.php that promotes a post to featured depending on how many times has been viewed. For the views I'm using a plugin method. Still I'm not able to loop through all the posts and set the sticky class.

function promote_post($viewed) {
global $post;

foreach($post->ID as $viewed){
   $post_views = wpp_get_views($post->ID);
}

if (is_home() && ($post_views > 2)){
        stick_post($viewed); 
  } else { 
         unstick_post($viewed)
      }
}
add_filter('post_class', 'promote_post'); 

I'm confident that I'm not using the foreach as it should be. I'd appreciate some directions thus to sharpen my PHP skills.

Thanks beforehand.

share|improve this question
What happens if you use foreach($viewed as $post)? – toscho Oct 3 '12 at 21:43
I get Trying to get property of non-object in functions.php on line 96, that is $post_views = wpp_get_views($post->ID); – user1672895 Oct 3 '12 at 21:59
Try get_the_ID() instead. Won't change anything, but there's a 1% chance that you got a typo somewhere. – kaiser Oct 4 '12 at 6:37
I get an error: Invalid argument supplied for foreach() – user1672895 Oct 4 '12 at 15:26
Thanks for the hints. This is the working code for reference: function promote_post($classes) { global $post; if (is_home() && wpp_get_views($post->ID) > 10){ stick_post(get_the_ID()); } else {unstick_post(get_the_ID()); } return $classes; } add_filter('post_class', 'promote_post'); – user1672895 Oct 4 '12 at 21:44

closed as too localized by Rarst Oct 14 '12 at 20:49

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.