Skip Navigation

[Resolved] Classify Existing Posts to Custom Taxonomy

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 1 reply, has 2 voices.

Last updated by Adriano 9 years, 4 months ago.

Assisted by: Adriano.

Author
Posts
#268043

J S

I am trying to: Assign both new & previously created custom posts (events) to newly created custom taxonomy (location-type) based on custom field values.

For example, people can submit events to our site via a Formidable Pro form. The form populates the custom post type "events", created with types. One of the custom fields of the events is "location", where a user selects from a drop down list of countries, but at the top of the list is "Virtual" for events that do not have a physical location. So users can either assign "Virtual" for events that do not have a physical location, or select a country if their events do take place in a specific location.

This works great, but now I'd like to be able to filter the events based on whether they are Virtual or Geographic. To do this, I created a custom taxonomy of "location-type" with two options, "geographic-event" and "virtual-event", and now I need a way to assign any previously created posts, and any newly created posts to either "geographic" or "virtual", depending on whether they have a country or "virtual" specified as the value in their "location" field.

I am not a programmer so a front-end solution would be ideal. If the only solution involves php, please be as specific as possible in the explanation. Likewise, if the solution involves a one-time cron to populate the previous posts, please be specific in how to set it up and then deactivate it so the process is not recurring unnecessarily. Can you also please advise on how to automatically assign the taxonomy when new events are submitted, based on the selection in the location field?

Thank You!

#268230

Hi jS-3,

I understand what you want to achieve. About the first thing, assigning posts to a taxonomy in one hit, after a quick search through WordPress plugins directory, I didn't find anything related to it. As it doesn't take so much time to be done, I've written a shortcode to help you with this task:

function wpv_assign_all_posts( $atts ){
	$a = shortcode_atts( array(
        'post_type' => '',
        'taxonomy' => '',
        'term' => '',
    ), $atts );

	$args['post_type'] = $a['post_type'];

    query_posts( $args );

	while ( have_posts() ) : the_post();
	    wp_set_post_terms( get_the_ID(), $a['term'], $a['taxonomy']);
	endwhile;

	wp_reset_query();
}
add_shortcode( 'wpv-assign-posts', 'wpv_assign_all_posts' );

This is just a simple shortcode that you can put in a single page or post to run. Three parameters are needed: post_type, taxonomy and term (name).

You should call it like this: [wpv-assign-posts post_type='employee' taxonomy="gender" term = "Males"]. This code should be in the content of any page and then run it.

It will assign all employee posts to a taxonomy gender in the term "Males". I've tested this already, but anyway I would recommend you to make a backup first.

The PHP code above should be in functions.php file of your active theme. Remember that this code will run as soon as you open the page containing the shortcode code in the front-end.

About the second question, it will depends of the available hooks of Formidable Pro. Some custom code will be needed, but you have to insert this code in some hook when the form is submitted. I don't know how this plugin works, but with CRED (our form builder plugin) you have some hooks available for this purpose.

So please have sure there is such hook and I will be more than happy in help you with this custom code.

The forum ‘Types Community Support’ is closed to new topics and replies.

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