Add meta field just after post is published - wordpress

I'm trying to add a custom field of event_month when a post is published or saved. I'm using the save_post action and getting the contents of a custom field containing the date and trying to store this in a separate custom field with just the month. This works perfectly when saving a post that has already been created. I've shown my code below.
add_action('save_post', 'update_event_date');
function update_event_date($post_id){
$post_type = get_post_type($post_id);
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
if ($post_type == 'tribe_events'){
$month = date("m",strtotime($event_datee));
update_post_meta($post_id, 'event_month', $month);
}
}
The problem arises when creating a new post. I think this is because the action fires before the _EventStartDate meta has been created and therefore the month can't be taken from this.
The hook is firing correctly and as intended when saving/updating a post but doesn't correctly get the month from the meta when creating a new post.
I'd really appreciate it if someone could provide me with some guidance.

To access post meta passed together with yours, you can do something like this:
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
foreach($_POST['meta'] as $meta){
if($meta['key'] == '_EventStartDate'){
$event_datee = $meta['value'];
}
};

Related

Updating an ACF field after creating a Woocommerce attribute

I have a set of products (courses) that are dependent on a calendar.
The calendar is generated by a post type date which has an ACF field date_start and an associated_product
The logic is; the user creates a post-type Date which adds an attribute term to the associated product's attribute pa_dates.
I have hooked save_post and so far so good, the attribute is written to the product.
I then need to copy the date field to the term created $term_set this section doesn't produce anything. (I need this date to order the terms in my product)
Part of the problem is I have no way of interrogating the variables as this is all happening in a hook that has no way of printing output. (I have tried fwrite but with no result)
// Add terms on Date save.
function add_terms_to_date($post_ID) {
if ('date' !== get_post_type() ) {
return;
} else {
$product_id = get_field('associated_product',$post_ID);
$term_name = get_the_title($post_ID);
$taxonomy = 'pa_dates';
$term_set = wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
// up to here, works fine.
// now need to identify that term ($term_set)
// Then I need to write copy the date field to its ACF field in my Date attribute
$selector = "term_date_start"; // the ACF field in the term
$date_start = get_field('date_start', $post_ID); // the date field to be copied
$value = $date_start;
// This next line caused the issue leading to the question so is commented out
// update_field($selector, $value, $term_set);
// and this line is correct
update_field($selector, $value, $taxonomy."_".$term_set);
}
}
add_action('save_post', 'add_terms_to_date');
So, complicated question, simple answer.
The last line should read;
update_field($selector, $value, $taxonomy."_".$term_set);
to avoid confusion with post IDs (RTFM) ACF has a different system for identifying terms. ($taxonomy comes from the earlier line and is pa_dates)
I have edited the post above, just in case it can provide help to someone.

Wordpress admin: Get current open post (order) edit page meta value

We have a website running on woocommerce and we run a custom tax solution there. On frontend everything works perfect atleast in admin as we have set, that tax is calculated by store address, we get the basic store country into all of the orders. What we are trying to achieve is to replace this country in admin from current open order post meta. But we are unlucky and we are not getting any data. Is there any possiblity to get current open post meta value?
function custom_base_country($base_country ){
if (is_admin()){
global $woocommerce;
$order = $_GET['post'];
$country = get_post_meta($order->id, 'country_code', true);
$base_country = $country;
}
return $base_country;
}
add_filter('woocommerce_countries_base_country', 'custom_base_country', 10, 1);
Found out the answer by changing the code to $country = get_post_meta($order, 'country_code', true);

Can't get post ID and category using wp_insert_post_data

I would like to create post titles automatically. The post title is made from the category and category ID before the post is created in the database but does not get the category and ID.
Any help will be much appreciated, Thank You.
function my_post_titles($data, $postarr){
$postid = $postarr['ID'];
$postcategory = $postarr['post_category'];
$data['post_title'] = $postcategory."-".$postid;
return $data;
}
add_filter('wp_insert_post_data', 'my_post_titles', '99', 2 );
Couple of things to be aware of here: wp_insert_post_data is the filter used by wp_insert_post to allow changes to its first parameter, so to understand your problem, you should check how wp_insert_post works.
https://developer.wordpress.org/reference/functions/wp_insert_post/
Essentially, if the post you are creating a new post, then it should not have any ID (as it's not even created and saved to the database yet). To have access to the post ID, I suggest you use save_post hook rather than wp_insert_post. save_post is the action triggered after the post is created or updated. For example:
add_action('save_post', function($post_id) {
$title = get_the_title($post_id);
if ($title) {
return; // if the post has already has a title, then do nothing
}
// Otherwise, update the post title here
});
Also, $postarr['post_category'] is an array, not a string, so to get the correct information, you must convert it to string before concat it with the post_id.

wordpress plugin creation get_post_meta

I am building my first plugin, and I am using as a reference the following link.
http://www.sitepoint.com/create-a-voting-plugin-for-wordpress/
and I am trying to underestand the following part of the code:
function voteme_addvote()
{
$results = '';
global $wpdb;
$post_ID = $_POST['postid'];
$votemecount = get_post_meta($post_ID, '_votemecount', true) != '' ? get_post_meta($post_ID, '_votemecount', true) : '0';
$votemecountNew = $votemecount + 1;
update_post_meta($post_ID, '_votemecount', $votemecountNew);
$results.='<div class="votescore" >'.$votemecountNew.'</div>';
// Return the String
die($results);
}
I run the code and it works, but I just dont understand the following:
What is "get_post_meta" doing?
Does it create a custom meta field, the same as add_post_meta?, if it doesnt why there is not an add_post_meta?
I checked the DB, and it looks like it is creating a custom meta field... so in that order what is the difference between get_post_meta and add_post_meta?
Thanks very much for helping me understand this.
The first time your code runs, get_post_meta returns '' so $votemecount is set to 0. The following update_post_meta creates the new meta field as documented below. Values that start with _ are not displayed (are hidden meta fields).
The function, update_post_meta(), updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.

How to show date with previous post link in Wordpress

When viewing a post, I want to show the date of the previous post underneath the previous/next post links but $post is not available in previous_post_link().
How can I carry forward the post date from the previous post as well as get the date for the next link?
Did a bit of searching on the web, but no result. Then I opened the file wp-include/link-tempalte.php.
On line 1327 you will find next_post_link which calls adjacent_post_link function.
This function again calls get_adjacent_post function to retrieve previous post data.
Looking at the source, you should be able to do the following:
$in_same_cat = false;
$excluded_categories = '';
$previous = true;
$post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
echo $post->post_date;
This is not tested, but I think it hould work.

Resources