Tag Info

Hot answers tagged

7

you don't need to loop through the values. Just use update_post_meta($post_ID, {key}, {array of vals}), it would do! <?php $poddata = Array( 'pod_id' => $this->pod_id, 'url' => $this->url, 'name' => $this->name, 'description' => $this->description, 'service' => $this->service, 'status' ...


5

The sticky posts are saved as an array of post IDs in the wp_options table. Hence, $stickies = get_option( 'sticky_posts' ); $stickies[] = $post_id; update_option( 'sticky_posts', $stickies ); will make the post in question sticky. EDIT: Even better, the core provides functions to stick and unstick posts (had to have 'em). stick_post( $post_id ); ...


4

In the function hooked onto save_posts you should make sure that you check that the action wasn't triggered by an auto-save routine. I suspect that the reason why the post 'forgets' the data is that the post auto-saves, and updates the post-meta with blank data. To do this: function save_details($post_id){ //Make sure you check this isn't an autosave. ...


3

To update post meta that are an array: you have to fetch the original values, change the value you need and update it again. For example $list_of_values = get_post_meta($post_id, '_list_values', true); if(!empty($list_of_values)) { $list_of_values["some_prop"] = "new value"; } update_post_meta($post_id, '_list_values', $list_of_values);


3

It appears the post meta is not being cleared for 'sunday' when the checkbox is unchecked. If( isset($_POST['sunday']) ){ update_post_meta($post->ID, "sunday", $_POST["sunday"] ); }else{ delete_post_meta($post->ID, "sunday"); } return $post; Or you can set the value to false If( isset($_POST['sunday']) ){ update_post_meta($post->ID, ...


3

First of all output your javascript in like this is a worse practice. So it's a lot better if you create your javascript file and the enqueue it using wp_enqueue_script hooked admin_enqueue_scripts action. function enqueue_my_ajax( $hook ) { if( 'post.php' != $hook && 'post-new.php' != $hook ) return; // if you use in theme use ...


3

preg_replace expects it's regex to be surrounded by a character. Usually that's a slash, like this: '/.*/' When you do this: preg_replace('%%PROPOSAL_LINK_URL%%', $some_text, $replacement); preg_replace thinks the first two % are your surrounding characters, and fails because of an unrecognized modifier. This is easy to test: $ php -a Interactive ...


2

update_post_meta() will update the value for the provided key if the key already exists in the database and then returns true, which is what you're seeing for subsequent calls. It only returns the ID of the post meta if the key didn't exist previously. If you want to store multiple values (rows) with the same key, use add_post_meta() instead.


2

The problem is that the function update_field from ACF works a little bit different from update_post_meta. Can you see it? update_post_meta( $post_id, $meta_key, $meta_value, $prev_value ); update_field( $field_key, $value, $post_id ); If you use update_post_meta you should in my opinion use get_post_meta to get the meta. If you use update_field you ...


2

it is much easier with WordPress inbuilt function media_handle_upload http://codex.wordpress.org/Function_Reference/media_handle_upload // These files need to be included as dependencies when on the front end. require_once( ABSPATH . 'wp-admin/includes/image.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); require_once( ABSPATH . ...


2

Unchecked checkboxes are not set in the $_POST, so you'd have to empty their meta field. Something like this should work : $sunday = ( isset( $_POST['sunday'] ) ) ? $_POST['sunday'] : ""; update_post_meta( $post->ID, 'sunday', $sunday );


2

use this into the loop or if you want to change created date to modified date than replace that with below code. As I know this feature works from all version of wordpress after 2.1 <?php the_modified_date(); ?> More detail you can find here http://codex.wordpress.org/Template_Tags/the_modified_date


2

I would simply filter the_title so that it outputs the appropriate custom-field data for your custom post type: <?php function theme_slug_filter_the_title( $title ) { global $post; if ( 'employee' == get_post_type( $post ) ) { $custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) : false ); $custom_title = ...


2

wp_update_post isn't used for updating custom fields (ie postmeta content) - you should use update_post_meta


2

(See codex) update_post_meta() calls update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value); This function is here. And there are plenty of hooks/filters: (In example of 'post', $meta_type='post') Before metadata is updated: update_{$meta_type}_metadata (here) Filter -If this returns anything other than 'null', the metadata won't ...


2

You will need to save them as an array and currently your HTML is not in the correct format to do this. <label for="my-cpt-32"> <input type="checkbox" name="cpt_ids[]" value="32" id="my-cpt-32" /> My CPT #32 </label> <label for="my-cpt-41"> <input type="checkbox" name="cpt_ids[]" value="41" id="my-cpt-41" /> My ...


2

There are two reasons I can think of that may cause negative impact wp_insert_post retrieves the post from the DB after writing it to it. this is not fun but should have only a constant impact, so I'm guessing it is not your main problem Each post is being stored in memory as part of the caching process, and in the end this makes your server choke when it ...


1

I'm not quite sure about what you want to do... Maybe it's this: add_action('save_post', 'update_thumb'); function update_thumb($post_id) { //verify post is not a revision if (! wp_is_post_revision($post_id)) { $new_value = 'testxxx'; update_post_meta($post_id, 'thumb', $new_value); } }


1

Wow thank you Nero_DCLXVI. Very helpful. For future people looking to implement this, here's simplified instructions of his solution: Add this hidden input along with your other custom meta inputs: <input type="hidden" name="prevent_delete_meta_movetotrash" id="prevent_delete_meta_movetotrash" value="<?php echo ...


1

It sounds like you are trying to directly save the altered information to the serialized value in the database. You can't do that. You have to extract the current meta data for that key so that you have and array or object, update that array or object, and then save the whole thing back to the database. Something like... $old_meta = get_post_meta( $pId, ...


1

I figured it out. Something was wrong with using '$id' in update_post_meta. I changed this: function ajax_posted() { $votes_plus = $_POST['votes']+1; $id = $_POST['post']; if (update_post_meta($id, 'votes', $votes_plus)) { $votes_ret = get_post_meta($id, 'votes', true); echo "Thanks for voting! ".$votes_ret." vote."; } else ...


1

The Core "trash post" links in the "Quick Edit" section on edit.php and in the "advanced" form in the "Publish" meta box work over GET not POST. Unless you have altered the form(s) somehow, there is no POST data. All you have is the post ID. To save data when a post is deleted, you won't be able to use the default "delete/trash" functionality. You will ...


1

Try this inside save_post but please note the code is not tested $old = get_post_meta($post_id, 'products'); $new = isset ( $_POST['products'] ) ? $_POST['products'] : array(); if ( empty ($new) ) { // no products selected: completely delete alla meta values for the post delete_post_meta($post_id, 'products'); } else { $already = array(); if ( ! ...


1

your nonce action name is wrong use same as entered in wp_nonce_field('update_grn_data','grn_data'); and also you forget to echo nonce, try this if ( isset( $_POST['grn_data'] ) && wp_verify_nonce($_POST['grn_data'],'update_grn_data') ) { //if nonce check succeeds. $postid = $_POST['post_id']; $data = $_POST['grn']; ...


1

Basically an ajax call within WordPress looks like this: The jQuery / JS script: jQuery.ajax({ url: ajaxurl, // the URL where the data are submitted type: 'POST' // the METHOD on how the data are submitted action: 'myaction', // the ACTION called to process the data data: { // the DATA sent to the server submit: 'somevalue', postid: ...


1

The third parameter of update_post_meta() can be an array. Changing the name attribute of the checkbox to something like "myCPT[]" and adding a value attribute: <input type="checkbox" name="myCPT[]" value="32"> My CPT <br> <input type="checkbox" name="myCPT[]" value="41"> My CPT 2 <br> <input type="checkbox" name="myCPT[]" ...


1

You can check the request value, before save and get an hint via wp_die() add_action( 'save_post','wpse46583_save', 10, 2 ); function wpse46583_save( $post_id, $post ) { // verify this is not an auto save routine. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; // You should check nonces and permissions here if ...


1

That will not work because you are referring to a single php file, instead the the whole WordPress environment. There's a easier way to use ajax in WordPress. Use admin_url('admin-ajax') as the form action, then put the function that will answer the ajax call in the theme's functions.php. More detail here in this answer


1

I figured I'll leave the meta values saving on multiple fields in the db, because I've been doing allot of pre_get_post query filtering. Some of the features in there are for searching/filtering on post meta data and would it start making things very difficult if I was to start saving values into doubly serialized values. A good habit to keep if I was to ...


1

Two issues with your code in current form: wp_insert_post_data hook runs before the actual insertion happens. In other words post might not not exist yet. Data in this hook does not contain post_id. More fitting hooks to use are those after post insertion is processed: do_action('save_post', $post_ID, $post); do_action('wp_insert_post', $post_ID, ...



Only top voted, non community-wiki answers of a minimum length are eligible