I wanted to change the author when a post change from draft to publish. I have $_GET['auth_id'] variable in post edit screen like this ...wp-admin/post.php?post=53&action=edit&auth_id=5. I tried save_post hook to change the post author like below function change_pos_auth($post_id){ if ( ! wp_is_post_revision( $post_id ) ){ // unhook this function so it doesn't loop infinitely remove_action('save_post','change_pos_auth'); if ( isset($_GET['auth_id']) ) { $args = array('ID'=>$post_id,'post_author'=>$_GET['auth_id']); // update the post, which calls save_post again wp_update_post( $args ); } // re-hook this function add_action('save_post','change_pos_auth'); …
add_action( "save_post_{CPT}", 'update_sticky', 15, 1 ); function update_sticky( $post_id ) { $stickies = get_option( 'sticky_posts' ); if ( ! in_array( $post_id, $stickies ) ) { $stickies[] = $post_id; update_option( 'test', $stickies); update_option( 'sticky_posts', $stickies ); } } It does work for test option I created for testing, but it doesn't work for existing sticky_posts option, although everything is the same. It does work for both if I hardcode the $post_id, like: $stickies[] = 123; Tried it in functions.php file, as …
I am trying to add more functionality to the update button. My end result is that I wish to call a python script but for now I am messing around with writing a log file. This is resulting in the log file being written to twice. Can someone help me? add_action('save_post', 'test',10,2); function test($id, $post) { if ($post->post_type != 'item'){ return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) { return; } if (wp_is_post_revision($id)) { return; } if (wp_is_post_autosave($id)) { return; } …
Hook save-post function to check if taxonomy term is attached or not. If attached then, use taxonomy term in post slug like "taxonomy-term-other-words"...
in a class I have a function for save_post action. In this function I send an email. All this works except for querying the categories for that post: class _new_notifications { public function __construct() { add_action( 'save_post', array( $this, 'send_new_post_notifications' ) ); } public function send_new_post_notifications( $post_id ){ $send_notify = get_field('send_notifications', 'option'); if ( !$send_notify || wp_is_post_revision( $post_id ) ) { return; } $categorie = get_the_category( $post_id ); $cat = $categorie[0]->name; //doing other stuff... I already tried everything possible (several …
The goal Update post title and slug on successful insertion into post table without re-triggering the save_post hook. Current behavior The function as it is, still triggers the save_post hook, even though I'm removing the action before wp_update_posts is invoked. When this code exists by itself, the code runs perfectly, however I have another piece of code that is triggered by the save_post hook, but only continues when the post is updated. When that function exists, the save_post hook that …
I was able to insert custom html into my gutenberg wordpress editor (hosted by bluehost) and test my code by preview with no problems. I made calls to google maps and fetch() to get a list of locations to plot on the map as markers. 2 days later, I was no longer able to save any javascript changes to my post. I tried deleting the post and starting a new post. I deleted my javascript code to insert something simple …
I'm troubled with a custom metabox, could someone give me a helping hand? I've got a custom post type which uses it, but unfortunately it's not storing the data into the database. Any clue? add_action( 'add_meta_boxes', 'beef_meta_box_add' ); function beef_meta_box_add() { add_meta_box( 'beef-metabox', 'Custom Product Settings', 'beef_meta_box_cb', 'product', 'normal', 'high' );} function beef_meta_box_cb() { global $post; $values = get_post_custom( $post->ID ); $price = isset( $values['beef_meta_box_price'] ) ? $values['beef_meta_box_price'] : ''; wp_nonce_field( 'beef_meta_box_nonce', 'meta_box_nonce' ); ?> <p> <label for="beef_meta_box_price">price n stuff</label> …
I have a custom post type with the following permalink structure /%match_season%/%match_team%/%postname%-%year%-%monthnum%-%day%/ %match_season% and %match_team% are taxonomies. The post type contains upcoming fixtures and past fixtures, with the post title set as the opposition name, giving me a nice url structure. However as the team names are the same each season I'm finding WordPress is adding a -2 to the %postname% in the permalink. I understand the reasons why WordPress adds a number to the end of a permalink, if …
Is there a way to trigger the post saving or updating event in code? such as do_action('save_post', $post_id); The function here seems to imply that a WP_POST object needs to be passed in. Is there a way to basically imitate the action of updating the post with all its existing values. The point is for the other hooks linked to all trigger on post update. Maybe do a wp_insert_post(), just passing in the post id? https://developer.wordpress.org/reference/hooks/save_post/ do_action( 'save_post', int $post_ID, …
I want to display a custom button in my "Edit post" screen which saves the current post (everything including custom fields) and then runs a function (in my case redirecting to another page). I know how to redirect after saving the post using the save_posts action and wp_redirect but I can't figure out how to save the post when a custom button is clicked. This is my function for redirecting using save_post (which doesn't work for me since I need …
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 things like post_name (not great if I need to restore from trash!). I have two thoughts but can't quite get across the finishing line with …
I want to store JSON in my post_content, but the kses filters makes it break. I would like to remove the kses filters when creating or updating (well, saving) a specific post type; anywhere in the code (thus using filters, I guess). How could I do that ? Thanks !
I have an old plugin, which used to work fine (years ago). Now I have to press publish twice to publich the post. The problem is in the blocks below. The used variables are declared above the code below. And there is no error there. Any effort to help will be much welcome. I have tried to use wp_insert_post instead. The error is still there. function prfx_meta_save( $post_id ) { if( is_null( get_post_meta( $post_id, $key = 'navn', true) ) ) …
I have an issue with WordPress update_post_meta() function. So I have made a custom input field for WooCommerce orders which hold Tracking number. Most of the time everything is running smooth and I am able to add and update this number. However on some rare occasions I find that the tracking number is not saved for the order which is open, but rather is saved for another order. What I mean by that. For example if my $item_id is 2000, …
I have a meta_key called "_ordre" and each time I create a new custom post, a new meta_value is set for that meta_key. The thing is, when I set the value of '_ordre' to 0, the meta_value is not saved into the database. Anything else is ok but 0. here is my code: function wp_audio_save_meta( $post_id, $post ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( !isset( $_POST['wp_audio_posts_nonce'] ) ) return; if ( !wp_verify_nonce( $_POST['wp_audio_posts_nonce'], plugin_basename( …
I have to execute custom PHP code after new post is saved with all it's meta data. My question is how to achieve that? Tried with save_post action, but it executes before meta records are saved, so I cannot use it in this case. So, how can I run my custom function after post with all related data is saved in database? UPDATED: I tried to achieve with next code in functions.php file: add_action( 'save_post', 'wpse41912_save_post' ); function wpse41912_save_post() { …