Unble to get save_post to work in wordpress - php

I have the following code to update the post_meta when a post is created. It is very simple,just storing the its own post_id in a meta field(might add more in the future)
The following code is not working, I guess it is because the $post_ID is blank, how do I pass the post_id of newly created post to the function update_postmeta (in function.php)?
//code from function.php
add_action('save_post', 'update_postmeta');
function update_postmeta($post_ID) {
update_post_meta($post_ID, 'related_id',$post_ID);
}

Here is a good boilerplate for you:
function update_postmeta($post_id) {
global $post;
// Post meta isn't sent for autosaves
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
update_post_meta($post->ID, 'related_id', $rand_id);
}
No where in your code is $rand_id defined though.

Related

Do an action when woocomerce saves a product

I need to save some options when a woocommerce product gets saved.
Is there any way to do it?
You could use save_post action hook!
save_postDocs
You could use this boilerplate to do your custom work when a product gets saved!
add_action('save_post', 'do_some_custom_work');
function do_some_custom_work( $post_id )
{
if ('product' == get_post_type($post_id)) {
// Do what is necessary here!
die('You hit the right hook!!!');
}
}
Note:
In addition to $post_ID, you could pass $post and $update to your callback function as well. Please read the documentation for more details!
add_action('save_post', 'do_some_custom_work', 10, 3);
function do_some_custom_work( $post_id, $post, $update )
{
if ('product' == get_post_type($post_id)) {
// Do what is necessary here!
die('You hit the right hook!!! This is the product id: ' . $post->ID);
}
}

WordPress "save_post" call only on save post not on update

I am trying to send post data to third-party services when the post is saved via API. I am using hook add_action( 'save_post', [$this, 'save_post_callback'], 100, 3); but this hook seems to be called in update post as well as post-new.php in admin panel. So to get rid of running this hook in post-new.php, I have checked the $_POST request but I am not able to filter the update post since I want to call API only in save the post, not in an update.
There seems to be the third parameter in callback function $update but it's not working either. Below is my code that needs to be called only in save a post but it's not working. Any help would be greatly appreciated.
function save_post_callback( $post_id, $post, $update ) {
// (!$update) => this doesnot seems to work
if(!empty($_POST) && $post->post_type == "post"){
//run only when save post
}
}
simple way is to check if _wp_http_referer last part is post-new.php or not.
here is a simple code
function save_post_callback($post_id, $post, $update)
{
// (!$update) => this doesnot seems to work
if ( ! empty($_POST) && $post->post_type == "post" ){
$end = explode('/', $_POST[ '_wp_http_referer' ]);
$end = end($end);
if($end == 'post-new.php'){
//echo 'it is new post';exit();
//do what you want here.
}
}
}
From StackExchange here. It looks like a clever way is to compare the post_date & post_modified to determine it is a new post.
$is_new = $post->post_date === $post->post_modified;
if ( $is_new ) {
// first publish
}

How to var dump each posts when moving to trash in wordpress

I'm trying to show a list of posts inside a var_dump, this is the actual code:
function deleted_cpt_orders() {
global $post_type, $post;
if ( $post_type == 'cpt_orders' ) {
var_dump($post);
}
}
add_action( 'trashed_post', 'deleted_cpt_orders' );
If i delete only 1 post the var_dump is shown, but if i delete 2 posts or more the result is
NULL
global $post is supposed to hold a single post object. If you delete multiple objects, it won't be set. However, trashed_post hook passes post id, therefore, you can do the following
function deleted_cpt_orders($object_id)
{
$post = get_post($object_id);
if ($post->post_type == 'cpt_orders')
{
var_dump($post);
}
}
add_action('trashed_post', 'deleted_cpt_orders');

Add data to post edit page, when post is published

How do I add some custom PHP to the "post edit"-page, and get the post_id, when the post is published?
Like this red area:
you can use get_current_screen() function (Link to codex) to check that you're on a post edit page and then get post id with a global $post variable.
Example:
add_action('admin_notices', 'screen_info');
function screen_info() {
$screen = get_current_screen();
if(is_admin() && $screen->id == 'post') {
global $post;
$post_id = get_the_ID();
// your code here
}
}

How to Fire an Event Only Once on WordPress Post Publish?

Thanks for any help with this.
I need to know how to hook wp_insert_post (or whatever is similar and better?) without it firing multiple times. What is the correct way to do this in WordPress?
For example:
In Plugin.php
add_filter( 'wp_insert_post', 'add_data') );
...
function add_data()
{
// This line is outputted twice
terseErrorLog("This code was executed.");
}
Try this:
function add_data() {
global $post;
if ($post - > post_status == "publish") {
terseErrorLog("This code was executed.");
}
}
add_action('save_post', 'add_data');

Categories