WordPress.org

Ready to get started?Download WordPress

Forums

Assigning a category to custom field value (4 posts)

  1. saggiatoreblue
    Member
    Posted 1 year ago #

    Hi. This may either be a stupid question or something that isn't possible. I have a client who wants a front end form, where people can submit a list of credits, each of which can be categorized as either 'film', 'television', 'commercial', etc. All of this is registered as a custom post type. I have setup some other fields with a custom meta box, however I am not sure if its possible to have a list of custom fields for each credit, and also the ability to choose a category for each field from a predefined dropdown list. Any help would be greatly appreciated!

  2. bcworkz
    Member
    Posted 1 year ago #

    If you can build a form in HTML, you can build a form in WP. It will take some coding to populate form elements with values from the DB if it is beyond the usual list_categories() sort of built in functions, but it is possible.

    One approach would be to develop a custom page template (the WP post type page, not any old PHP page) that contains the form. When the form is submitted, the form fields are validated and sanitized before inserting them into a new post via wp_insert_post(). Note the page template is just the container for the front end form. We are still creating an actual post of your custom post type. The new post can still be edited in the backend via the usual custom post type edit admin panel.

    Once the template is complete, add a new page based on this template. There is likely no need to add any content, just assign a title so the page can be permalinked by the title. Be sure to apply any needed security so not just anyone can add posts, unless that is the intent. Publish the page. If the template was setup correctly, everything should now work.

  3. saggiatoreblue
    Member
    Posted 1 year ago #

    Thanks bcworkz. I do have that setup. However my main issue is creating the ability to categorize individual fields rather than the whole post. So like, for example I will have 12 text fields, and next to each would be a dropdown that would contain a set of categories, so you can assign a category to an individual field. Im not sure if this is even possible in the wordpress db. Thanks.

  4. bcworkz
    Member
    Posted 1 year ago #

    Yes, it's possible. You may be able to use wp_dropdown_categories(). If that doesn't work, use get_categories() and step through the returned array and output the required HTML in any manner that you need.

Topic Closed

This topic has been closed to new replies.

About this Topic