How to insert category and subcategory using ‘wp_insert_post’ function?

The argument description in the Codex entry for wp_insert_post() has exactly what you need (reformatted). ‘post_category’ => [ array(<category id>, <…>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post’s categories So, per the Codex. Use wp_set_post_terms(). wp_set_post_terms( $post_id, array( 1, 2, 3), ‘category’, true ); For custom taxonomies: ‘tax_input’ => array( ‘taxonomy_name’ … Read more

wp_insert_post to schedule a post – but nothing happens?

As Rarst mentioned I had to use wp_update_post(), but then there is a little trick – one must also set edit_date = true otherwise it has funny behavior. The final code looks like this: function schedule() { $postdate = date(‘2014-06-11 01:00:00’); $postdate_gmt = date(‘2014-06-11 05:00:00’); $post = array( ‘ID’ => 11, ‘post_status’ => ‘future’, ‘post_type’ … Read more

What hook should be used to programmatically create a post only when master post is updated?

you can use pre_post_update action hook like so: add_action(‘pre_post_update’,’post_updating_callback’); function post_updating_callback($post_id){ global $post; // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE ) return; if ($post->post_status == “publish”){ //do update stuff … Read more

wp_insert_post disable HTML filter

You could use call kses_remove_filters() before saving and call kses_init_filters() afterwards, but pay attention it will also remove filtering from title, excerpt and comments, So what you should do is just unset the content filters. // Post filtering remove_filter(‘content_save_pre’, ‘wp_filter_post_kses’); remove_filter(‘content_filtered_save_pre’, ‘wp_filter_post_kses’); and after the post is saved // Post filtering add_filter(‘content_save_pre’, ‘wp_filter_post_kses’); add_filter(‘content_filtered_save_pre’, ‘wp_filter_post_kses’);

How can I programmatically save data into custom fields that contain serialized data?

For serializing and unserializing Data in WordPress, you can use maybe_unserialize and maybe_serialize. If you want to handle the data and add/change values, you could use something like this: $metaarray = maybe_unserialize(get_post_meta($post_id,’_ait-dir-item’,TRUE)); //now $metaarray has an array, if it is serialized. If not, $metaarray has a string. if(is_array($metaarray)){ $metaarray[‘mynewfield’] = ‘Some text i need to … Read more

Set Primary category using the Yoast SEO plugin

Try instead update_post_meta($post->ID,’_yoast_wpseo_primary_category’,$childid); Use below function: function wpseoPrimaryTerm($taxonomy, $postID, $term){ if ( class_exists(‘WPSEO_Primary_Term’) ) { // Set primary term. $primaryTermObject = new WPSEO_Primary_Term($taxonomy, $postID); $primaryTermObject->set_primary_term($term); // Save primary term. $primaryTermObjectAdmin = new WPSEO_Primary_Term_Admin(); $primaryTermObjectAdmin->save_primary_terms($postID); }else{ echo ‘Class WPSEO does not exit’; } } Where $taxonomy – taxonomy name, $PostID – $post->ID, $term – $childid

Programmatically add terms to custom post types

I find out the problem and the solution. I debugged the “wp_set_object_terms” by using “is_wp_error” and I got “Invalid Taxonomy” message, then I realized that when the posts was being created that term didn’t exists. So I change the hook to “init” in the programmatically_create_post() function, and voila! Below this line the code working: <?php … Read more

Assign category to front end post

I checked and it works with this: $new_post = array( ‘post_content’ => $_POST[‘post-content’], ‘post_title’ => wp_strip_all_tags( $_POST[‘post-title’] ), ‘post_type’ => ‘custom_pt’ ); note: I used a text input for the single category <input id=”input-name-value” name=”input-name-value” type=”text” /> I havent tried it with a select – option dropdown. or more than 1 category. $post_id = wp_insert_post( … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)