When I publish or update a post, I want to save the slug of the post in an ACF field nammed "plan_slug".
The code I wrote is:
add_action('future_to_publish', 'actions_published_post');
add_action('new_to_publish', 'actions_published_post');
add_action('draft_to_publish' ,'actions_published_post');
add_action('auto-draft_to_publish' ,'actions_published_post');
function actions_published_post($post) {
update_field('plan_slug', $post->post_name);
}
Nothing runs good! The field in post back-end is not fullfilled...
Can you, please, tell me what is wrong in my code.
Thank you in advance.
this should work
add_action( 'save_post', 'save_custom_field', 10, 2);
function save_custom_field( $post_id, $post ) {
update_post_meta( $post_id, 'plan_slug', $post->post_name );
}
Related
I have a Custom Post Type "Compania", it has a meta field called "compania_id" i would like that field = current post_id
I implemented this action to do that and it works when i create a new post (compania_id automatically is equal to current post_id) but in older compania post when this code hasnt been created, when i edit these post and save, compania_id isnt filled with the post_id as when i create a new post, why??
function anadir_post_id($post_id){
update_post_meta($post_id, 'compania_id', $post_id);
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 1);
The hook you use has three parameters. Therefore your code needs to look like this.
function anadir_post_id( int $post_ID, WP_Post $post, bool $update ){
update_post_meta( $post_id, 'compania_id', $post_id );
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 3 ); //<--- 3 params!
If you have an older version of php it will look like this:
function anadir_post_id( $post_ID, WP_$post, $update ){
update_post_meta( $post_id, 'compania_id', $post_id );
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 3 ); //<--- 3 params!
And, of course, if you have access to a post's wp_postmeta rows, you already know its ID. What you store here may be redundant.
Using ACF; I am making a plugin that hooks into WP on 'draft_to_publish' and on 'pending_to_publish' this part works fine. But when I try to get the ACF generated fields they return as blank, as if the fields generated by ACF haven't been set yet.
Short version of my plugin looks like this:
function scheduelMailChimp( $post ) {
// get post data and preb it for mail
$post_ID = get_the_ID();
$content_post = get_post( $post_ID );
$content = $content_post->post_content;
$postTitle = get_the_title( $post_ID );
//log debuginfo til debug.log
log_me(
array(
'get field date' => get_field($field_name, $post_id, $format_value)
)
);
}
add_action( 'draft_to_publish', 'scheduelMailChimp', 10, 1 );
add_action( 'pending_to_publish', 'scheduelMailChimp', 10, 1 );
The above code outputs empty. If I try to output something that is generated by WP and not ACF everything works like a charm.
all and any bright ideas are more then welcome :)
In case other people are looking for the same answer: After diving into a ton of Google searches, it seems that ACF haven't saved the data at the time draft_to_publish is fired. So I tried using the 'save_post' and it worked like a charm.
In WordPress with the publish_{post_type} (http://codex.wordpress.org/Post_Status_Transitions) you can hook into the loop if a post with custom type is published. But publish_{post_type} is also triggers if a a post is updated.
I am basically looking for a way to check the old and new status, within the publish_post_type hook. Does anyone have a nifty idea as to how to accomplish that?
I am doing this:
function doStuff( $post_ID ) {
// do stuff
return $post_ID;
}
add_action( 'publish_ttplaned', 'doStuff' );
So basically I need to check the old and new status of the post within the fuction doStuff().
In case someone else is looking for the aswer. I ended up using Williams solution.
add_action( 'draft_to_publish', 'doStuff', 10, 1 );
add_action( 'pending_to_publish', 'doStuff', 10, 1 );
function doStuff( $post ) {
if ( get_post_type( $post ) == "my_custom_post_type" ){
// do stuff
}
}
This works since a new post starts out as a draft even if you publish it right away.
As #rnevius says, there's no hook in WordPress core called publish_post_type.
There is also publish_post - which I've just tested it to make sure it only runs when the post is first published, not when it's subsequently edited.
So you can do something like:
function my_function($post_ID, $post) {
if ("foo" == $post->post_type) {
// do something
}
}
add_action( 'publish_post', 'my_function', 10, 2 );
There is also the wp_transition_post_status(), which calls several actions:
transition_post_status with $new_status, $old_status, $post
or, if you want to do it the other way around:
{$old_status}_to_{$new_status} and passes a WP_Post object as the only parameter.
hoping for some advice.
I'm programmatically inserting a large number of posts into wordpress from a JSON feed. The wp_insert_post function is working brilliantly and the posts are created, along with correctly populated Advanced Custom Fields meta data.
We have a strange issue by which until we manually click "update" on a single post the custom fields aren't available using a JSON API plugin.
I've tried updating all via the bulk editor, as well as calling wp_update_post after the JSON import. It's as if the act of clicking "update" on a single post saves the post in a different fashion.
Can anyone advise why this would be the case? Any advice or pointing in the right direction would be greatly appreciated!
EDIT: the code we're using to update our post meta...
function __update_post_meta( $post_id, $field_name, $value = '' ) {
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}
So we resolved the particular issue we were having.
When a post is programmatically created with ACF fields, the posts are not labelled in the same way as standard post meta. Read here for more info on that!
Updating a post manually creates the necessary "aliases". Until that point, if you want to get the info out, you need to reference ACF's initial "fieldXXXXXXXX" post meta key.
Is there any way to be registered to wordpress events?
I want to know when a post was updated or created in the system, and get its ID.
Is it possible?
Thanks
You can use the save post hook. It is triggered when the post is created and updated.
Here's a sample.
function post_hook( $post_id ) {
if ( wp_is_post_revision( $post_id ) ){
//Post is being updated
} else {
//Post is being created
}
}
add_action( 'save_post', 'post_hook' );
Here's a link from the codex.
Save Post WP