Questions tagged [save-post]

save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. The data for the post is stored in $_POST, $_GET or the global $post_data, depending on how the post was edited.

Filter by
Sorted by
Tagged with
21 votes
5 answers
26k views

Prevent post from being published if custom fields not filled

I have a custom post type Event that contains a starting and ending date/times custom fields (as metaboxes in the post edit screen). I would like to make sure that an Event cannot get published (or ...
englebip's user avatar
  • 439
38 votes
1 answer
15k views

Why does save_post action fire when creating a new post?

I'm surprised by the fact that my function that I've tacked onto the save_post action fires when I click the "New Post" link in the Admin Dashboard. Note - this is before I've pressed Save or Update, ...
Tom Auger's user avatar
  • 7,036
25 votes
11 answers
41k views

Check for update vs new post on save_post action

Is it possible within the save_post action to determine whether it's a new post being created or an existing post being update?
hereswhatidid's user avatar
22 votes
3 answers
16k views

How to avoid infinite loop in save_post callback

I have been using this site alot for solving my problems, but this time I did not succeed in finding and answer for my problem. I get and infinite loop when using wp_update_post inside a function ...
elgehelge's user avatar
  • 385
4 votes
2 answers
9k views

Retrieve 1st image in post and set it as featured image, when post is saved/updated

I need to set a featured image for every post I publish. So, when a post is published/updated, the script would scan the post for the first image using a script like this: // Get URL of first ...
Gixty's user avatar
  • 1,097
37 votes
2 answers
86k views

what is correct way to hook when update post

i try to hook when post is updated but all hook i try never executed except updated_post_meta add_action('updated_post_meta', 'my_function'); function my_function($post_id) { echo 'This is ...
rusly's user avatar
  • 473
22 votes
4 answers
14k views

Passing error/warning messages from a meta box to "admin_notices"

I have a simple meta box that updates the post custom fields (using update_post_meta()). How can I send a error or warning message to the next page after the user publishes/updates the post and doesn'...
onetrickpony's user avatar
  • 13.6k
8 votes
4 answers
13k views

Force post slug to be auto generated from title on save

I'm running a property site where many properties are sold in apartment blocks. Because of this what the content editors do is create a post/property with all the details and then use a duplicate ...
Scott's user avatar
  • 12.2k
8 votes
3 answers
16k views

Set custom messages for post update/save

I am trying to make a custom message instead of the default message when i save a post, does anybody know how to do it!
Karl Barton's user avatar
31 votes
7 answers
36k views

Add validation and error handling when saving custom fields?

I have a function that defines a custom field on a post type. Say the field is "subhead". When the post is saved, I want to do some validation on the input, and display an error message on the post ...
MathSmath's user avatar
  • 5,578
26 votes
4 answers
27k views

Using save_post to replace the post's title

I am using custom posts, and in these, I don't a need for the title. This causes Wordpress to set the titles of my posts to "Auto Draft". I'd like to change the title's value to something else, ...
Tsahi Levent-Levi's user avatar
8 votes
2 answers
5k views

Return $post_id when DOING_AUTOSAVE?

I see the following pattern over and over, on this site and on other places: add_action( 'save_post', 'wpse14169_save_post' ); function wpse14169_save_post( $post_id ) { if ( defined( '...
Jan Fabry's user avatar
  • 30.4k
0 votes
1 answer
336 views

Minimum Word Count Before A Post Can Be Made Pending Review

I'm looking for a way to check that a post has a minimum word count before allowing it to be sent for review (added to the pending post status). Below is the code I have: if (current_user_can('...
Peter Willis III's user avatar
4 votes
2 answers
10k views

How to force function to run as the last one when saving the post?

I don't know how to force this function to run as the last function after all other save things from WP are executed. This is my current code in my plugin: add_action( 'save_post', 'do_custom_save' )...
Derfder's user avatar
  • 2,052
2 votes
1 answer
4k views

Check before publishing, if already exist post with current custom field value

I have a custom post type with only 3 custom fields in it. $post_types = get_post_meta($post->ID,'post_types',true); $post_taxonomies = get_post_meta($post->ID,'post_taxonomies',true); $...
user13250's user avatar
2 votes
1 answer
580 views

Better post meta efficiency?

I work with websites which require using Post Meta for Post Objects within a particular Post Type. I often add a Meta Box to a particular Post Type, to provide additional settings for the new Post ...
Michael Ecklund's user avatar
2 votes
3 answers
10k views

Is there an easy way to AJAX-ify saving of post?

We have a plugin that allows us to manage custom post types and we'd like to add AJAX functionality in saving, editing, and deleting posts. I couldn't find similar problems in the internet so I'm ...
jilseego's user avatar
  • 317
1 vote
2 answers
3k views

update_post_meta() not working in save_post

I have added some text input fields to metabox in my custom post. Now whenever I put some data in those text fields in admin panel and click 'save', all data vanishes. Here is the code: <?php ...
HoGo's user avatar
  • 347
1 vote
1 answer
4k views

Save selected item from dropdown menu in a meta box as a metadata value for a custom post type

I have been beating my brains on this one for days now and my Google-fu is failing me. I have created a custom meta box with a dropdown menu that lists all of the terms from a custom taxonomy. What I ...
Burgon's user avatar
  • 98
0 votes
1 answer
2k views

How do I save each option in a multiple select menu as it's own meta_key + meta_value pair?

How do I save each option in a multiple select menu as it's own meta_key + meta_value pair? This is the basic multiple select menu. Note: multiple select menus allow you to select more than one ...
Andrew's user avatar
  • 1,163
0 votes
1 answer
662 views

Custom field values get deleted

I created a custom post type:products which also has custom fields: price & shipping. Occasionally, if I leave the edit product window open or refresh it, I lose the values inside price and ...
MF1's user avatar
  • 285
6 votes
5 answers
6k views

Unable to prevent function using save_post firing twice

I am using save_post for a function to send an email when a post is updated by a user. This is firing twice and I am aware this is due to the post revisions and autosaves. I have tried to prevent ...
Ben H's user avatar
  • 363
6 votes
4 answers
28k views

Execute action after post is saved with all related post_meta records (data)

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 ...
user198003's user avatar
6 votes
1 answer
24k views

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. $...
eskimo's user avatar
  • 446
5 votes
2 answers
6k views

Why won't my metabox data save?

I have a custom post with a lot of meta boxes. I recently tried to add some dynamic metaboxes, using this question: Create more Meta Boxes as needed I managed to get the metaboxes to add nicely, the ...
gillespieza's user avatar
  • 1,275
5 votes
6 answers
20k views

Custom post type save_post action not firing

Sorry if this is already answered on here. I looked around and couldn't see any answers to my question so I thought I'd post my own. I'm building a plugin for a client that gathers customer feedback ...
JamieCassidy's user avatar
4 votes
2 answers
1k views

Submitting posts from the front end - sanitizing data

I have a form that allows any registered user to submit a post (it's a custom post type - forum related), and I'm using wp_insert_post() to insert it into the db. From what I've seen in this function ...
onetrickpony's user avatar
  • 13.6k
4 votes
1 answer
4k views

save_post + insert_post = infinite loop

I'm trying to automatically create a new post when a post containing a certain custom field is saved - hooking an insert_post function into save_post. This generates an infinite loop. I found some ...
ptriek's user avatar
  • 857
3 votes
2 answers
2k views

Get updated post meta on save_post action?

I'm trying to save post id and keyword (Yoast Focus Keyword) to a custom table when post is created or updated via save_post action. It saves everything fine on Publish (first-time) but when I update ...
Key's user avatar
  • 135
3 votes
2 answers
3k views

Get updated meta data after save_post hook

I am trying to get the updated value of a piece of meta when a post is saved (custom post type) but when I try to grab the updated data on the save_post hook, I get the previous data instead. I tried ...
Eckstein's user avatar
  • 1,099
3 votes
3 answers
1k views

Non-hierarchical custom taxonomy using checkboxes on edit-screen -> saving issue

I registered a non-hierarchical custom taxonomy ('property_features'). There is a checkbox for every terms of this tax in a custom meta box on the edit-screen. All terms are displayed and all terms ...
LichtAnd's user avatar
3 votes
1 answer
6k views

Why is my custom meta box input not saving

I've added the following function to functions.php to add a custom meta box enabling the end user to input a gallery shortcode. I then intend to use this to place a gallery in a specific place on the ...
Raskolnik's user avatar
  • 143
3 votes
3 answers
8k views

A Post is saved twice or more during add_action(save_post)

I have added a custom meta box for advanced information for a specific post category, to my create new post page. Now I recognize, that if I save the post, it writes 2 entries in the db with 2 ...
mybecks's user avatar
  • 351
3 votes
1 answer
2k views

Return code from save_post action?

I'd like to understand what value I should return from a WordPress save_post action function. This example from the save_post documentation both explicitly and implicitly returns without a value: ...
Matt Gibson's user avatar
3 votes
3 answers
3k views

WP Rest API - Upload media without saving attachment post

I need to hook after file uploaded to server, get the file path, and then prevent WordPress from saving the attachment post. I found this filter add_filter('attachment_fields_to_save', '...
BenB's user avatar
  • 795
2 votes
2 answers
3k views

How can I pass $post object to 'save_post' add_action?

I have several functions that are called from inside a save_post function. However, all of the functions that use the $post object are returning incorrect values because it appears that the default ...
Scott B's user avatar
  • 5,666
1 vote
2 answers
3k views

Update post on save

I am trying to work out how to change details of a post after its status has changed. I would like to change the title of the post by appending the post id number to it. Here is the code that I'm ...
Sam Miller's user avatar
1 vote
3 answers
5k views

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 ...
Chris_O's user avatar
  • 20.6k
1 vote
1 answer
457 views

How to auto-update child posts whenever the parent post is updated?

To clarify, this question is merely on how to "save" or "update" a child post when the parent post is saved. Nothing more. Hello to all of you brilliant and wonderful people. I need to auto-update ...
SHA3.org's user avatar
  • 119
1 vote
1 answer
4k views

How to change the post author when the post is published?

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 ...
rram's user avatar
  • 195
1 vote
1 answer
8k views

How can i create a function to get youtube video time

Tom J Nowell posted this code to help me retrieve the YouTube video time. Functions // function to parse the code from the URL function parse_youtube_video_id_from_url( $video_url ) { $...
Batman's user avatar
  • 145
1 vote
1 answer
147 views

Change 2 fields in the post depending on the 3rd field before save

I have a custom content type called cards. I have 3 custom meta fields called: my_cards_activity (select field type; options 0 and 1) my_cards_user (select type; many options) my_cards_datetime (...
Derfder's user avatar
  • 2,052
1 vote
2 answers
1k views

Save checkboxes of a metabox with a foreach loop (invalid argument)

I'm new to Wordpress plugin development and I'm trying to create a list of checkboxes (from a WP Query) to associate as custom fields for a specific post type. Nothing wrong with the display function, ...
Alessio's user avatar
  • 13
0 votes
2 answers
7k views

Set Primary category using the Yoast SEO plugin

function set_primary_on_publish ($post_id) { global $post; $categories = get_the_terms( $post->ID, 'category' ); // wrapper to hide any errors from top level categories or products ...
Dylan Smit's user avatar
0 votes
2 answers
2k views

Remove image from post_content on save_post

I'm trying to setup a function in functions.php that removes an image from the post_content on save. Everything works up until the time I'm trying to save the content, the page just hangs and I get a '...
jyoseph's user avatar
  • 197
0 votes
2 answers
3k views

wp_insert_posts Fatal error: Maximum function nesting level of '100' reached, aborting!

I'm having a weird problem I have written the code as below: add_action('save_post', 'save_post_func'); function save_post_func(){ include_once(ABSPATH . WPINC . '/feed.php'); $rss = ...
Antonio Papa's user avatar
0 votes
1 answer
990 views

Post meta checkbox becomes unchecked occasionally

I removed the default author meta box on posts and added it back with a checkbox that I use to determine if a post should show the author's byline and author box. It's been working, but for some ...
notGettingStabbed's user avatar
0 votes
1 answer
2k views

"Notice: Undefined index:" error when adding new content?

I'm getting the following error... Notice: Undefined index: album_tracks_metabox_nonce in /Applications/MAMP/htdocs/TheToasters/wp-content/themes/Toasters/discography-post-type.php on line 124 ......
Brad Adams's user avatar
0 votes
1 answer
67 views

Metabox of one post influence setting on other

This great answer by David Gard allowed me to have a metabox sticky (checkbox) for posts that could only be set (checked) on one posts in the loop at a time. If post A has that meta box checked, then ...
Bram Vanroy's user avatar
0 votes
1 answer
4k views

prevent post submission

I'm using a meta-box for custom post type. I have three problems: WP seems to run the function that I attached to the "save_post" action, every time a new post is opened for editing or reloaded. Is ...
Matanya's user avatar
  • 227