Skip Navigation

[Resolved] conditional user has post with parent current page

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

Problem:
How to count posts a certain user has created, and unhallow the user to create more then one of the given Posts?

Solution:
1. Create a CRED Form with which you let the user create your post.

2. Display the Link to that CRED somewhere on your site

3. Create a Custom PHP function to evaluate the amount of Posts created by the currently logged in user.

/**
 *Count posts of given type, so each user can create post only once
 */
function u_post_count() {
    $user_post_count = count( get_posts( array( 
    'post_type' => 'your_post_type_slug', 
    'author'    => get_current_user_id(), //this will count the posts of the current logged in user
) ) );
         
    return $user_post_count;//this returns a numerical value of Posts created by that user
}

4. Register this function in Views > Settings > Compatibility > 3rd Party Functions

5. Use it in a wpv-conditional as elaborated here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/ > Using Custom Functions Or Class Methods

6. Wrap the CRED Form / link with above conditional

Optional:
You can access also the Parent of the Current Post by querying the meta_key "_wpcf_belongs_your-parent-post-type-slug_id" and meta_value "Post ID" (the ID of the parent Post)

So your code can look like this:

/**
 *Count posts of given type, so each user can create post only once
 */
function u_post_count() {
    $post_id = $post->ID; //get current post ID
    $user_post_count = count( get_posts( array( 
    'post_type' => 'your_post_type_slug', //child post type
    'meta_key'         =>  '_wpcf_belongs_your-parent-post-type-slug_id',
    'meta_value'       => $post_id,
    'author'    => get_current_user_id(), //this will count the posts of the current logged in user
 
) ) );
          
    return $user_post_count;//this returns a numerical value of Posts created by that user
}

Relevant Documentation:
https://codex.wordpress.org/Template_Tags/get_posts
https://codex.wordpress.org/Template_Tags/get_posts
https://codex.wordpress.org/Function_Reference/get_current_user_id
http://php.net/manual/en/function.count.php
https://toolset.com/documentation/user-guides/creating-cred-forms/

This support ticket is created 8 years, 1 month 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by justinB-5 8 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#363826

I am using CRED with a CPT that is a parent of an event to allow subscribers to add their name to an interest list. The problem I'm having is that subs can currently add their name multiple times. I would like to use wp-conditional to:

(1) Only allow a subscriber to "vote" once, and
(2) Show a "remove from list" button if they have already "voted" on that parent post

Would a conditional be the best way to accomplish this? If so, can anyone help me with syntax on this?

Thanks!

#363886

Does your CRED Form with which the Subscriber "Votes" actually crate a Post, correct?

You can use a Custom Function to check the amount of Posts that a certain (logged in) user has created in a given Post Type.

If that's above 1, you don't display the CRED Form/Link to it anymore.

The Delete Button is easy to display with the CRED Delete Post Link.
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred-delete-post-link

Here is the Custom Function, which you can use in a Conditional afterwards:

/**
 *Count posts of given type, so each user can create post only once
 */
function u_post_count() {
    $user_post_count = count( get_posts( array( 
    'post_type' => 'your_post_type_slug', 
    'author'    => get_current_user_id(), //this will count the posts of the current logged in user
) ) );
        
    return $user_post_count;//this returns a numerical value of Posts created by that user
}

You will need to register the Function in Views > Settings > Compatibility > 3rd party Functions and use it in a wpv-conditional as elaborated here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/ > Using Custom Functions Or Class Methods

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#363888

Yes that is correct, it does create a new post (vote). So will this function count only the posts for the current parent? The subscriber needs to be allowed one post (vote) per parent (event).

Thanks!

#363893

I am not sure how you tell CRED to create Posts that are PARENT of a given post, as usually you do the opposite, you create CHILD of a post with CRED.

Anyway, my function gets the posts of a given type, by a given author and counts them

It doesn't matter if you count parents, Childs or default WordPress Posts, it will always only return the amount of posts, that a certain AUTHOR created, in a given Post Type.

I don't see the necessity in your case to get / count parents or childs of certain given posts.

You will need to adapt that function, please read the inline comments and the WordPress Codex in regard, if you want to extend it:
https://codex.wordpress.org/Template_Tags/get_posts
https://codex.wordpress.org/Function_Reference/get_current_user_id
hidden link

If you need to specify also the Parent, you can get_posts() where the Custom field _wpcf_belongs_parent-post-type-slug_id is equal to your Parent Post.
This is how Types stores Child / parent relationships.
The Parent does not hold any information, the Child does hold the parent Post ID in the above mentioned Field.
You can extend the get_posts() by a meat_key and meta_value argument, as you can see in the WordPress Codex.

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#364208

This code works, but the issue that I brought up is there. This code causes the following:
If a user "votes" on one event, it is counted globally so they don't have the option to vote on other events. That's why I brought it up as counting only the posts that are a child of the current page.
Any thoughts?

#364323

You can, in the function I provided, add any arguments you want.

Currently, it's a WP Query with get_posts only using post_type and author.

You can access the Parent of the Current Post by querying by meta_key "_wpcf_belongs_your-parent-post-type-slug_id" and meta_value "Post ID" (the ID of the parent Post)

This is the link to the WordPress get_posts() query:
https://codex.wordpress.org/Template_Tags/get_posts

So your code can look like this:

/**
 *Count posts of given type, so each user can create post only once
 */
function u_post_count() {
    $post_id = $post->ID; //get current post ID
    $user_post_count = count( get_posts( array( 
    'post_type' => 'your_post_type_slug', //child post type
    'meta_key'         =>  '_wpcf_belongs_your-parent-post-type-slug_id',
    'meta_value'       => $post_id,
    'author'    => get_current_user_id(), //this will count the posts of the current logged in user

) ) );
         
    return $user_post_count;//this returns a numerical value of Posts created by that user
}

This should count only posts which are Child of the current post.

Please acknowledge that this is custom programming work which is beyond the scope of our support.

At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#364325

I certainly acknowledge that, and I also certainly appreciate your help greatly.

Thanks again 🙂

#364326

Oops, forgot to mark as resolved.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.