Hot answers tagged

13

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


9

I would advise you not to use the user meta. Although your problem is caused by: update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta update_post_meta( $author_id, "_author_follow_count", ++$author_follow_count ); note that you called update_post_meta not update_user_meta, so somewhere, there's a post whose ID ...


8

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


4

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);


4

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


4

Your approach is good, you should run a basic check against the data tough. Otherwise you might get an array or a serialized object … which might lead to unexpected consequences (including security issues!) when you or someone else is trying to print the values. Maybe something like this is easier: $key = '_my_meta_key'; $value = filter_input( INPUT_POST,...


4

Yes it does. Escaping depends on context and in worst case like using esc_html when writing directly to the DB are just a security hole. Even if there is no security issue, there is theoretical one. The user asked you to store A, and you are storing B. In a simple cases B is exactly how A should be displayed in the HTML, but life is rarely simple and while ...


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

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


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 . 'wp-admin/...


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

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

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

It's all going into the same table, you're possibly not using the correct key name to insert it. The only thing that differentiates custom field keys and keys used for metaboxes is that metabox keys are typically prefixed with an underscore to hide them from the list that shows up in the custom fields section. it's also possible to store multiple things as ...


2

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


2

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


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


2

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


2

the _edit_lock is generated each time you edit a post or page. it consist the timecode and the user. so WordPress is knowing who is currently editing it. meta_id post_id meta_key meta_value 9 5 _edit_lock 1388386997:1 if you manipulate it WordPress reacts somehow sensitive ...I tried to fetch how many seconds somebody ...


2

global $current_user; get_currentuserinfo(); if (isset($_POST['email'])) { wp_update_user( array('ID' => $current_user->ID, 'user_email' => $_POST['email']) ); unset($current_user); get_currentuserinfo(); //just in case } echo '<p>You'll recieve a mail here: ' . $current_user->user_email ...


2

I found the solution by the comment from birgire what tells to use wp_defer_term_counting() as follows: if( ! $errors ) { // Set wp_defer_comment_counting(); wp_defer_term_counting( true ); foreach( $lines as $line ) { // Import post $args = array( 'post_title' => $new_vipost_title,...


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){ wp_nonce_field('first_name','...


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



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