Skip Navigation

[Resolved] Only author’s first post’s extra fields are shown

This thread is resolved. Here is a description of the problem and solution.

Problem:

Display custom fields value in a layout of single post.

Solution:

You can edit the layout and add an visual editor cell, and display the custom fields value.

Relevant Documentation:

https://toolset.com/documentation/user-guides/rich-content-cell-text-images-html/

This support ticket is created 5 years, 9 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)

Author
Posts
#1214939

My site shows custom posts made by subscribers (role Subscriber). Now if the subscriber buys a subscription (WooCommerce Subscription) his role is changed to Customer and he gets some extra fields for the custom posts. Another benefit to change role is that a Subscriber can write only 1 custom post but a Customer can write 3 posts.

Site visitor can see the posts of the subscribers and also extra fields are visible, IF the post author is a Customer.

To build this I have some custom coding, which I’ve googled – since I don’t do PHP – and for some I got help here, thank you! Now I need help again 🙂

My problem is now, that when the Subscriber is changed to Customer and he can write the second post and the second post also has the extra fields, these extra fields are not visible for visitors (the fields are visible in editing form though). Same thing with third post. Only the first post’s extra fields are visible for visitors.

So, here’s how everything is done.

1. The role change is done with WooCommerce Subscriptions and it seems to work ok.
2. The new posts are done with Toolset Forms (cred), extra fields are shown with Toolset conditions, seems to work ok.
3. Restricting of post amount by role is done with this code (seems to work ok):

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;

//validate if specific form
if ($form_data['id']==559)
{
$user = get_current_user_id();
$user_post_count = count_user_posts( $user , 'muusikko' );
$user_data = get_userdata($user);
$user_role = $user_data->roles;

if ( $user_role[0] == 'subscriber' && $user_post_count > 0)
{
//set error message for my_field
$errors['wpcf-user-validation’]=‘Only one profile’;
}

if ( $user_role[0] == 'customer' && $user_post_count > 2)
{
//set error message for my_field
$errors['wpcf-user-validation’]=‘Only three profile.’;
}
}

//return result
return array($fields,$errors);
}

4. The shortcode to check post autho’r role is done with this code, perhaps this is the devil here:

function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
add_shortcode( 'user_role_func', 'get_author_role' );

and in content template the shortcode is used like this:

[wpv-conditional if="( '[user_role_func]' eq 'customer' )"]
here goes the extra fields
[/wpv-conditional]

Do you find the failure, why is only the first post’s extra fields shown in frontend not the second’s and third’s? Their author is after all the same Customer role author than the first one’s.

#1215252

Hello,

This is a custom codes problem.

I suggest you check these:
1) in your content template, output the custom shortcode [user_role_func] directly, check the it's result in front-end, make sure it does output value "customer" correctly.

2) If it does not output correct value for for the second post and following posts, then it should be a problem of "get_author_role", you will need to manually debug the custom PHP codes for shortcode [get_author_role]

For example, you can get current post's author ID with WordPress function get_the_author_meta():
https://developer.wordpress.org/reference/functions/get_the_author_meta/

Then get the user's roles with function get_userdata() by author's ID:
https://codex.wordpress.org/Function_Reference/get_userdata

#1215360

Thank you Luo.

Point 1: the output is correct, "subscriber" for those role's users posts and "customer" for those role's users posts, even multiple posts.
Point 2: Perhaps you meant to say here "If it DOES output correct value"...?

#1215363

No, I mean it does not.

If they does output correct user role values in front-end, then the problem should in your codes as you mentioned above:

... here goes the extra fields ...

How do you setup/output those "extra fields values"?
If they are custom user fields of current post's author, you can use shortcode [wpv-post-author] to output the user field values, see our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author

#1215376

Ok. The "extra field values" are custom post fields (as said "extra fields for the custom posts"), not user fields.

#1216694

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug, thanks

#1216766

I assume we are talking about the custom PHP codes "get_author_role" here:
hidden link

Please provide more details:

Where and how can I test that shortcode in front-end?
Should I login as user "testi"?
Please point out the front-end URL

Where and how do you setup the "content template", please point out the content template URL, thanks

#1216779
3b.png
3a.png
2b.png
2a.png
1.png

Please make this reply private also, thank you.

"Where and how can I test that shortcode in front-end?"
Log in frontend (footer right) as testi/testi

"Should I login as user "testi"?"
Yes please. Attached you find image 1 which explains what you see after that.

"Please point out the front-end URL"
This one profile has the extra fields visible under image:
hidden link (check attached image 2a and 2b)

These two don't:
hidden link (check attached images 3a and 3b)
hidden link
Event though they are filled in the form.

"Where and how do you setup the "content template", please point out the content template URL, thanks"
Layout "Muusikkosivu" id 586.
Url hidden link

#1216811

I don't see any attached image in your post:
https://toolset.com/forums/topic/only-authors-first-posts-extra-fields-are-shown/#post-1216779

I have checked the layout you mentioned above, there isn't shortcode [user_role_func] in it, so I have add a visual editor cell in it at the bottom, copy paste the same codes as you mentioned above:

<p>[user_role_func]</p>
<hr />
<p>[wpv-conditional if="( '[user_role_func]' eq 'customer' )"]<br />
here goes the extra fields<br />
[/wpv-conditional]</p>

Login as user "testi", test all three posts in front-end:
It works fine, see them here:
hidden link
hidden link
hidden link

All output the same result:

customer

here goes the extra fields

It works fine.

Please describe detail steps to duplicate the same problem:
that when the Subscriber is changed to Customer and he can write the second post and the second post also has the extra fields, these extra fields are not visible for visitors (the fields are visible in editing form though).

Thanks

#1216813

OK, I can see the screenshots now, checking it, will update here if there is anything found

#1216842

Here are what I found:
I assume we are talking about the post view "PRO-data":
hidden link
I can see the shortcode [user_role_func] in section "Loop item in PRO-data"

There are some issues:
1) above post view is querying posts of post type "Muusikot"
filter by:
Select posts in Any relationship that are related to the Post where this View is shown.

2) and you are displaying above post view in a single "Muusikot" post
There is the problem, there isn't any post type relationship between "Muusikot" posts, and you can not setup direct post type relationship in same post type, there isn't such a built-in feature within Toolset.

So it will conduct the result: no item found

I assume you are going to display custom fields of the current "Muusikot" post, if it is, you don't need the the post view "PRO-data", you can move those codes in content template "Loop item in PRO-data", to layout "Muusikkosivu" directly, I have tried these in your website.

Edit layout "Muusikkosivu", add a visual editor cell at the bottom, copy/paste the same codes into cell content
hidden link

test it in front-end, see the results at the bottom of below pages:
hidden link
hidden link
hidden link

I can see the social links without any problem, please correct me if there are other misunderstandings.

#1216855

Thank you Luo! I don't quite get all you say 🙂 and I still wonder why the one post did everything right and other two didn't - but I'm really happy it now works as it is supposed to. So big warm thank you again!