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!
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.