Good morning
I have a problem with organizing a custom post. For an annual event last year I created a custom post "events" that had taxonomy "days and places". More than 450 appointments were divided into 3 days and many places.
This year I have to repopulate it with other appointments: I would not want to lose the historian but I need to separate the management.
I thought of creating a taxonomy "edition" and filter them, but the problem arises of having everything however confused. In fact, the days would become 3 of last year and 3 of this, the places may not be the same.
Before taking a solution like creating a new custom post "events 2018" or something similar, I wanted to ask you if there was a more correct solution.
thank you
Fausto
Hi, I can think of a few approaches that may work, depending on how you want to organize your site and Event posts. The simplest is to add a custom field to the Events post type that stores a year, like "2018". Then in any Views or WordPress Archives that show Events, add filters that only display posts with the custom field value "2018". Posts without the 2018 year value will not appear in View results. The original URLs to their single post pages will still exist and be live, but those posts will not appear in Views or WordPress Archives loops because you have filtered them out. If you decide to do this site again next year, you need only change the filter values to "2019". This field value could even be automatically set whenever you publish a post, by writing a few lines of custom code. I could assist with that if you'd like.
This is a very basic example that may need to be adjusted for complex scenarios, but it's difficult for me to be able to predict what those might be on your site. It will probably take some experimentation to expose those issues, so I would encourage you to clone your existing site to a new staging URL and try your modifications there before you commit to any one approach on the live site. If you run into any roadblocks, feel free to let me know what we need to address.
Hello and first of all thanks for the reply.
So in this case it is better to use a custom field than a taxonomy?
If you explain to me how to insert the year automatically it would already be a good solution.
The next problem is that the events are loaded via CREED form. So when I ask to choose between a day (which is a taxonomy) I also propose the dates of last year. Is it possible to exclude them in the form creed?
The last question that I would have opened (I think) before making the changes is the visualization in the backend: is it possible to filter at this point only the events of a year?
I know it's a lot of things but I'd like to be sure to take the right path.
Thank you!
Fausto
So in this case it is better to use a custom field than a taxonomy?
I'm not sure, a taxonomy may work well. It depends on many factors. Custom fields may be easier to use in parametric searches of Events, but taxonomies come with their own automatic archive URLs, like yoursite.com/taxonomyslug/termslug. WordPress provides these automatically for all taxonomy terms, but not for custom fields. I think you should try both and see which you prefer.
So when I ask to choose between a day (which is a taxonomy) I also propose the dates of last year. Is it possible to exclude them in the form creed?
No, that is not possible in CRED for a single taxonomy unless you create a Generic Field and use custom code with the CRED API. Another option is you could create a new taxonomy called Day-2018, include this new taxonomy in the CRED form, and delete the old Day taxonomy from the CRED form. The terms from last year's Day taxonomy would not conflict with the new Day-2018 terms.
If you explain to me how to insert the year automatically it would already be a good solution.
If all Events are created by CRED, you can use the CRED API. First, create a Number custom field on the Events post type called "eventyear". Then insert this code in your child theme's functions.php file:
add_action('cred_save_data', 'set_eventyear_custom_field',10,2);
function set_eventyear_custom_field($post_id, $form_data) {
// change this number to match your CRED form ID:
if ($form_data['id'] == 12345)
{
$year = date('Y');
update_post_meta($post_id, 'wpcf-eventyear', $year );
}
}
Change 12345 to match your CRED form's numeric ID, and you will find that all Events created by this CRED form have the Event Year custom field automatically set to the current year. If you decide to use a taxonomy instead, we can create similar code that applies the correct taxonomy terms automatically.
Hi Christian
I have not decided yet how to proceed but I think that edition will have to be a taxonomy, so I would ask you when you have time if you help me to prepare the code to insert in appropriate function.php.
I still have many doubts whether it is sensible to do so or to empty the events, but at this point I will proceed by trial.
Thanks for everything
Fausto
Could you clarify what is needed in the functions.php file?
- What is the slug of the 'edition' taxonomy?
- What edition taxonomy term should be applied automatically?
- What is the slug of that taxonomy term?
- Are all Event posts created by CRED?
- Can Event posts be edited by CRED?
Hi
the slug of taxonomy is "edizione"
the term is "2018"
the slug's term is "2018"
All event is created by creed
The post can be edited by creed or by backend
Thank you so much
Fausto
Please add the following code to your child theme's functions.php file:
function assign_year_term_to_event ( $post_id ) {
$post_type = get_post_type( $post_id );
$post_status = get_post_status( $post_id );
if ( $post_type == 'event' && $post_status == 'publish' ) {
$term_args = array('fields' => 'slugs');
$ed_terms = wp_get_object_terms($post_id, 'edizione', $term_args);
// if the year term already exists, leave it
if(sizeof($ed_terms) == 0)
wp_set_object_terms($post_id, date('Y'), 'edizione', false);
}
}
add_action( 'save_post', 'assign_year_term_to_event', 100 );
If your Events post type slug is not "event", please replace "event" with the correct slug.
This code updates the edizione terms associated with an Event post any time it is published, and any time a published Event post is edited. This code is triggered either by wp-admin or by CRED. If there is already an 'edizione' term associated with the post, the code will not do anything. This is because you don't want to add this year's term to last year's Events if you happen to update those Event posts during another year. If no 'edizione' terms are currently associated with the post, the current year term is added.
Hi Christian
i put it in function.php child, but seems not working
I have a doubt. "Edition" is a taxonomy, not a term's field, it's correct the code?
I have a doubt. "Edition" is a taxonomy, not a term's field, it's correct the code?
Yes, it's correct. The code is set up to work with the taxonomy 'edizione', and the current year '2018' as a term in that taxonomy. The code here associates the current year date('Y'), in the 'edizione' taxonomy, to the post being saved.
wp_set_object_terms($post_id, date('Y'), 'edizione', false);
- Did you add or edit an Event to test the code? Try both and let me know if there is a difference.
- What was the post status of the Event you edited?
- If any 'edizione' terms are already added to the Event post, this code will not do anything. It's designed to work with only one term from the edizione taxonomy assigned to an Event post. Please remove any edizione terms from the post you want to edit and try again.
- If it still does not work, I may need to take a closer look in your wp-admin area to see how things are configured. Please provide login credentials here in the private reply fields if necessary.
in creed I set the status of custom posts created in this way on draft.
The code is set up to apply the term when the post is "published", so that could be why it appears not to work. If you want to test it with draft posts, modify line 4 in the PHP code I provided above to be:
if ( $post_type == 'event' && ($post_status == 'publish' || $post_status == 'draft' ) ) {
Please publish an Event to test it as-is, or modify the code as shown to work with draft Events. Then test again. Let me know the results.
I still having problem with the code, but now i have another problem more important un the form
When guest try to upload a image the form says "There was an error uploading your file." . From administrator it works
I try to search in access and other parts but i did'nt find any answer.
THank you
Fausto
Let me know when you are ready to continue. I will mark this ticket as pending an update from you. No need to reply right away. The ticket will stay open for 30 days.