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

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

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


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

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

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

(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

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

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


1

Iterate over all posts, find the attached images … $images =& get_children( array ( 'post_type' => 'attachment', 'post_mime_type' => 'image' ); … and for each image use wp_update_post() to set the date from the parent post.


1

$article_date = $custom["tf_book_author"][0]; $article_date = $custom["tf_book_afirst"][0]; $article_date = $custom["tf_book_isbn"][0]; $article_date = $custom["tf_book_dts"][0]; $article_date = $custom["tf_book_price"][0]; This is your issue. You need to set these to $tf_book_author and such, especially if you're going to echo them. I stopped there, so ...


1

I've got a loose implementation for you. Look into the 'wp_insert_post_data' filter, it is the last filter/action executed that has access to the data being inserted. You should be looking at receiving just the first parameter, which will be the array of data, changing that, then returning it. You will have access to all the post data that is about to be ...


1

A Note: Actions and Filters are not really interchangeable: Filters typically must return the passed data or it's going to break something. Building off of what m0r7if3r said, wp_insert_post_data is a filter, so you should be modifying the post's $data and returning it at the end of the function. (Alternatively, you could global the variables you need to ...


1

I know this is question was asked a while ago, but just incase someone was wondering, an easier way of doing this would be: <?php $date = get_post_meta($post->ID, "cfc_date", true); echo the_time('Y-m-d' , $date );?> Check out Wordpress Codex


1

The issue is that you are using get_post_meta(), but since you're hooking wp_insert_post_data you have not set the post meta yet (because the hook fires before the post is inserted into the database. You should be pulling from the metadata submission, and running through any filters you use for update_post_meta(). If you post the code where you set the ...


1

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


1

Here is some code that I've used to add posts from a front end form that also adds meta data and taxonomy terms. Note that snippet extracts all of the security, data validation and data sanitization. It just show what I do with the data to add it to the database once I've sanitized it all. // Submit the values if there are no errors if(empty($errors)) { ...


1

Maybe if you use add_post_meta instead, because update_post_meta assumes that you have already that value and you are updating it ( from http://codex.wordpress.org/Function_Reference/update_post_meta ). Also check if $doc->prod[$i]->awImage is actually a string (do print_r instead of echo).


1

It seems that the action save_post gets fired when sending a post to the trash... Therefore, my custom metadata save function was being activated with no $_POST data to send thru it. To circumvent this, I wound up adding a nonce to the save function. function wpg_testimonial_author() { global $post; $custom = get_post_custom($post->ID); ...


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

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


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

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

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


1

This will fire on init so you just need to refresh a page one time for it to work. I left out the actual value of $new_value -- you need to define that. <?php add_action( 'init', 'wpse_75308_update_video_meta' ); function wpse_75308_update_video_meta() { // Get all posts $posts = get_posts( array( 'numberposts' => -1 ); // Loop through ...



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