Skip Navigation

[Resolved] Conditional if Author has posts

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 3 voices.

Last updated by Luo Yang 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#556617

Ian

Tell us what you are trying to do?

I want to create a conditional statement to show something if the author of the current page has any posts of a specific post type.

if "the author of current page"
has any posts "recipes" a custom post type
show "some content"

#556630

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - you need to use custom shortcode that returns the count of posts based on current author id.

Please add following function to your current theme's functions.php file:

function func_count_author_posts() {
   global $post;
   
   $author_id = $post->post_author;
   $post_type_slug = 'student';    // Replace your post type slug with 'student' word
	      
   return count_user_posts( $author_id , $post_type_slug );
   
}
add_shortcode( 'count_author_posts', 'func_count_author_posts' );

Where:
- Replace your post type slug with 'student' word

More info:
=> https://codex.wordpress.org/Function_Reference/count_user_posts

And then try to use conditional shortcode as given under:

[wpv-conditional if="( '[count_author_posts]' gt '0' )"]
    show your content
[/wpv-conditional]

Where:
- Please register shortcode name 'count_author_posts' at:
=> Toolset => Settings => Front-end Content tab => Functions inside conditional evaluations

More info:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
[Section: "Checking Custom shortcodes"]

#556684

Ian
has-posts-snip.jpg
has-recipe-snip.jpg
settings.jpg
conditional-output.jpg
output.jpg

The author of the page has two posts and one recipe, but I am getting no output.

Here is a link to the page hidden link
This is a local install so it may be a little slow.

Here is the link to the authors recipe hidden link

#557133

Dear Lan,

In your case, I don't think it need custom codes, I suggest you try to create a view for it. For example, create a postview :
1) Query posts of your custom post type
2) add a "Post author filter":
Select posts with the author the same as the page where this View is shown.
3) In section "Loop Output Editor", insider shortcode

	[wpv-items-found]
...
	[/wpv-items-found]

display the content you want.

More help:
Filtering Views Query by Author
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

#557235

Ian

I have a loop built already. It is wrapped in an accordion element on my page. I am trying to hide the whole accordion if the loop is empty.

You can see the accordions here hidden link

#557493

Yes, as I mentioned above in post:
https://toolset.com/forums/topic/conditional-if-author-has-posts/#post-557133
Step 3) In section "Loop Output Editor", insider shortcode [wpv-items-found]...[/wpv-items-found]
display the content you want.

You can display the "whole accordion" insider the [wpv-items-found]...[/wpv-items-found]