0

I'm trying to make a conditional if the current author has uploaded a featured image in a custom post, but it ain't workin' ...

<?php if ( 
1 == count_user_posts( get_current_user_id(), "CUSTPOSTTYPE" ) 
&& is_user_logged_in() 
&& has_post_thumbnail()
) { ?>
5
  • You mean if the post has a featured image no matter who upload it right? The has_post_thumbnail function just checks if the post has a feature image, not if the current author upload it.
    – Laxmana
    Feb 6, 2017 at 12:31
  • if the current logged in author/user has published a custom post with a featured imaged attached to their post
    – Pete
    Feb 6, 2017 at 12:35
  • Also I believe that your logic is wrong based on what you said you are trying to do. count_user_posts returns the number of posts the author posted. So you are saying if the user has posted one post (no matter which post) and if is logged in and if the post has a thumbnail.
    – Laxmana
    Feb 6, 2017 at 12:36
  • It works for me so far... i'm just trying to add the featured image condition on top of what works
    – Pete
    Feb 6, 2017 at 12:38
  • Working do not imply correctness.
    – Laxmana
    Feb 6, 2017 at 12:39

2 Answers 2

0

Try this

<?php
if ( 
1 < count_user_posts( get_current_user_id(), "CUSTPOSTTYPE" ) 
&& is_user_logged_in() 
&& has_post_thumbnail()
) {
?>

// if(TRUE,TRUE,TRUE)

to debug, you should check the output of the following...

var_dump(count_user_posts( get_current_user_id(), "CUSTPOSTTYPE" ));
var_dump(is_user_logged_in());
var_dump(has_post_thumbnail());
4
  • What is that? I want it to equal 1 post. Please check your answer's code.
    – Pete
    Feb 6, 2017 at 12:29
  • Then use 1 === operator not 1== Feb 6, 2017 at 12:35
  • how is your code different to what I showed in my question?
    – Pete
    Feb 6, 2017 at 12:41
  • It is different at 1<count... as 1== True is equal to 1==1... Feb 6, 2017 at 17:00
0

The condition in my answer says that if the user is logged in and if the current user is the user that posted the current post and if that post has a featured image then do something.

if(is_user_logged_in() 
&& get_current_user_id() === get_the_author_meta( 'ID' )
&& has_post_thumbnail()) {
    /* action */
}
8
  • I don't want the current post, I want the current user's custom post. This conditional needs to work in any theme file, as mine does now.
    – Pete
    Feb 6, 2017 at 12:53
  • What do you mean by user's custom post?
    – Laxmana
    Feb 6, 2017 at 12:54
  • The custom post that the current author published. Trust me it works.
    – Pete
    Feb 6, 2017 at 12:54
  • The code above works for custom posts. You are in a single custom post page right? When you check with count_user_posts you just check how many post the user has posted it. So if the user has posted more than one your condition will fail. That's why Vishal put a greater condition, to catch that scenario. Sorry if I insist but I am trying to help.
    – Laxmana
    Feb 6, 2017 at 12:59
  • My original code worked in any template file to restrict the current logged in user accordingly, it didn't only apply the condition when it was on a specific cpt page or otherwise. Thanks for helping and hanging in there ;)
    – Pete
    Feb 6, 2017 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.