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

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

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

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 ...


3

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

Try with this code, //Metabox Hook add_action('add_meta_boxes','stars_meta_box'); //Metabox Init function stars_meta_box(){ add_meta_box('first_name_meta_box', 'First Name','first_name_meta_box_html','spark_stars','normal','default');); } //Metabox Html function first_name_meta_box_html($post){ ...


2

meta_id in postmeta table is just an AUTO_INCREMENT id for the table, you can easily inspect that: So, your guess is correct, you can leave it. EDIT Avoid raw SQL statements and introduce $wpdb for SQL purposes. And for postmeta insertion you can see the following tutorial I found a best one: Reusable Custom Meta Boxes - Code.TutsPlus.com


2

You should not be using a direct SQL. Consider creating a PHP script which loads WordPress core and uses proper functions (see http://stackoverflow.com/questions/9101503/include-wordpress-core-into-own-scripts#answer-9126230 for getting an idea how to do that). add_post_meta update_post_meta But if you do want to create custom SQL (if you are asking the ...


2

You would have to do the following to save the order data into the WordPress DB, Do a WordPress AJAX sending the data that you want to be stored in it. You can do it at every re-arrange event of the elements. Handle the AJAX call in PHP and either do an update_option() or an update_user_meta() to save the data.


2

Slight change in your code, function wpse_custom_field_on_publish( $new, $old, $post ) { // Only run on "from X > to publish"-transitions if ( $new === 'publish' && $old !== 'publish') { $page_views = get_post_meta( $post->ID, 'post_views_count', true ); // Only set 'post_views_count' post meta value if there is none ...


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

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


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

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

(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

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

I don't understand your attempt to use PHP_Exec, especially by embedding it in the content section of the page. That is going to run much too late to alter anything in the <head> of the document. Here is the problem you are facing, starting from what looks to be your primary question: So what I need to do is understanding how Wordpress is adding ...


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']; ...



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