Articles of save post

Undefined 'post_type' error on Add new page

I created a meta box for page see the following code: add_action(‘add_meta_boxes’, ‘add_metaboxes’); function add_metaboxes() { add_meta_box( ‘wpt_slider_meta’, ‘Add Slider Image’, ‘wpt_slider_meta’, ‘slider’, ‘normal’, ‘default’ ); } function wpt_slider_meta() { global $post; wp_nonce_field(plugins_url(__FILE__), ‘meta_slider_noncename’); $values = get_post_meta($post->ID, ‘_meta-slider_img’, true); // Some html and scripts to upload image. } add_action( ‘save_post’, ‘save_slider_img’ ); function save_slider_img( $post_id […]

Exclude trash from save_post

I have a function which processes custom metabox data on saving my custom post type: add_action(‘save_post_customtypehere’,’myown_save_customtype_fn’); function myown_save_customtype_fn($ID) { … } However, the function also runs when I trash items within this CPT (I guess it’s effectively saving the post to change post_status to trash). Without the metabox being present, my function ends up clearing […]

get post meta before it is updated (during SAVE_POST)

Lets say I have custom field on post editor, and I change value from AAA to ZZZ.. : add_action(‘save_post’, function($post){ $value = get_post_meta($post->ID, ‘mykey’); } , 1); How to get the old value (AAA) of that meta-key? during save_post (even earlier 1st priority), I get ZZZ

Is it possible to create a post using a metabox?

I am trying to create a post (well, in the end, multiple posts) from a metabox using the ‘save_post’ hook. I executed the script once, creating 3900 or so new posts before I crawled into posts.php and discovered that wp_insert_post() calls the ‘save_post’ hook. Does anyone have a creative solution around this to create the […]

Call add_action() in function wordpress

I have a problem with a WordPress hook. I want to call an action in another actions callback, but it doesn’t seem to work. I want to call add_meta_tag action only if the page is saved. This is what I have: function saveCustomField($post_id) { add_action( ‘wp_head’, ‘add_meta_tag’ ); } add_action( ‘save_post’, ‘saveCustomField’ ); function add_meta_tag(){ […]

Faking the “onSave” event

While a specific use-case, I believe this may benefit others. I am using TurboCSV to mass-import a number of posts into WordPress (which it is doing wonderfully so far). Within that import, I am specifying custom fields to be used in conjunction with Advanced Custom Fields to store/output the data. The post_meta table is being […]

Conflicting save_post functions when passing the post id and saving custom meta boxes for different post types

Post save functions are conflicting with each other when adding them to the save_post action hook. 2 different custom post types with 2 different (one for each post type) custom meta boxes. I’m only including the code for 1 of the meta boxes. The other one is very similar and each one works fine separately […]

save_post action firing before I publish / save the post

I’m trying to update a posts meta only after it has been saved or updated. My function is pretty simple function update_test( $post_id ) { update_post_meta($post_id, ‘copied’, ‘1’); update_post_meta($post_id, ‘blurb’, ‘this value updated by save_post action’); } add_action( ‘save_post’, ‘update_test’); When I add a new post at wp-admin/post-new.php I can see the two custom fields […]

Wp_update_post: Infinite loop even with remove_action solution, OOP

I have a quite complex PHP class, reponsible for creating and printing meta_boxes, generating and saving meta fields, … Each meta_box (object of class) has its own save function, that updates meta data of post (fields for them are instances of another class) as well as its post_excerpt and post_content. Yep, it’s probably a little […]

update_post_meta and update_field ony working when saving the post

I use Advanced Custom Fields (ACF) to store data in a custom field. I had latitude and longitude information stored separately, but I they need to be in one custom field so I made the function below. $query just queries all the relevant posts. <?php $query = query_custom_posts(); if($query->have_posts()): while ($query->have_posts()): $query->the_post(); if(get_field(‘map_lat’) && get_field(‘map_lng’)): […]