Skip Navigation

[Resolved] Limit amount of custom posts within a custom taxonomy

This support ticket is created 4 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Jamal 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1805117

I'm looking to limit a custom post type (Preferred Agents) to a maximum of 3 posts within a custom taxonomy (Locations). So only 3 preferred agents are allowed within a single location. Is this possible with Toolset? I've looked all over Google, but can't find anything. My PHP knowledge is decent but limited, so I'm not able to just write something off the top of my head. Any help with this would be awesome, especially if it can be done with Toolset 🙂

Thanks!

#1805771

Hello and thank you for contacting the Toolset support.

This requirement(limiting posts per taxonomy term) can't be done without some custom PHP code.

Can you explain how do you let your users create "Preferred Agents"? From the backend or from the frontend using a Toolset Form?
If you are using Toolset forms, you may hook into "cred_form_validate" and trigger errors if needed.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#1813189

Hi Jamal, thanks for the response.

Users currently would use a front end Toolset form to sign up as a preferred agent.

#1813567

Thank you for your feedback.

As you may know, taxonomies cannot be assigned to users. By design, taxonomies can only be assigned to the default posts, or to any custom post. We overcome this limitation by creating a custom post type(let's say Preferred Agents) that will link to users using the author field. So, each user will be the author of his post(Preferred Agents).

So, when you let users sign up using a Toolset form, you will need a way to let them create their posts. So, each user will have only one "Preferred Agents" post. Then, we can assign a taxonomy(Locations) term to these posts.

Currently, in Toolset, each form can only create one item. A User form can only create one user, and a Post form can only create one Post.

If we want to create a "Preferred Agents" post for a user upon signup, we'll need to create custom code that will hook into the cred_save_form of the signup User form.
Or, we can allow the user to create his "Preferred Agents" post using a Post form. In this case, you will need to hook into the cred_form_validate of the Post form, check how many existing posts are assigned the location that the user has chosen, and if the location has already 3 posts, you can return an error to the user saying that this location can't be added anymore.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Or you can hook into the cred_save_data, check if the location has already 3 posts, and remove it for the newly created post.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Check our documentation about both hooks, and you can find examples in the previous tickets, on the forum, using the following scoped searches:
- hidden link
- hidden link

I hope this helps. Let me know if you have any doubts or questions.