Tag Info

Hot answers tagged

35

You can set an image as post thumbnail when it is in your media library. To add an image in your media library you need to upload it to your server. WordPress already has a function for putting images in your media library, you only need a script that uploads your file. $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $filename = ...


11

Use wp_set_object_terms after you have the post id for each taxonomy: ... $post_id = wp_insert_post($my_post); wp_set_object_terms($post_id,$location,'location'); wp_set_object_terms($post_id,$sale_rental,'sale_rental'); wp_set_object_terms($post_id,$price,'price');


9

You kind of answered the question yourself already, Create a function that will create the 3 posts ex: function create_new_user_posts($user_id){ if (!$user_id>0) return; //here we know the user has been created so to create //3 posts we call wp_insert_post 3 times. // Create post object ...


7

The short answer; absolutely. wp_insert_post() will only SQL escape the content. Use the KSES library & wp_kses() to filter out the nasties, or esc_html() to escape all HTML. Most importantly, check out the codex on data validation (read: sanitization). A Note On KSES: Use wp_filter_kses() or wp_kses_data() to apply the same KSES rules as post ...


6

That's pretty strange issue which everyone will face especially if they call this function inside some loop <foreach> <for> <while> etc. You should try this if (!get_page_by_title($title, 'OBJECT', 'post') ){ $my_post = array('post_title' => $title, 'post_content' => 'Content', ...


6

Try using set_post_thumbnail(). Edit by Otto: You clarified your question, so I'll clarify the response Chip gave. Basically, you need to make the 'attachment' for the post as well. When an image is uploaded into the WordPress media library, a special post entry is made for it with a post type of attachment. This attachment is linked to some specific post ...


5

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


4

Here's a way to do it with PHP's array map function: // Good idea to make sure things are set before using them $tags = isset( $_POST['tags'] ) ? (array) $_POST['tags'] : array(); // Any of the WordPress data validation functions can be used here $tags = array_map( 'esc_attr', $tags );


4

When using function like wp_insert_post() It will work on the currently active blog, meaning that if you are on a blog in your network with blog_id of 2 the the post you insert using wp_insert_post() will be inserted to that blog. Now if you want to insert the post to a specific blog you can use switch to blog() function which will tell WordPress that ...


4

To save the link in the post meta you can use update_post_meta like this for example: $url = "http://sample.com/entertainment/default.aspx?tabid=2305&conid=102950" $my_post = array( 'post_title' => "$title", 'post_content' => "$content", 'post_status' => 'draft', 'post_author' => 1, 'post_category' => array(1), ); ...


4

Right there is no big difference between them, actually wp_set_post_terms() uses wp_set_object_terms() but does few extra check for you. That's also noted on wp_set_object_terms() Codex page: Perhaps the wp_set_post_terms() is a more useful function, since it checks the values​​, converting taxonomies separated by commas and validating hierarchical ...


4

Simply put the wp_insert_post call inside your conditional check so its only called if the post title is not empty, something like this: if (empty($_POST['my_title'])){ echo 'error: please insert a title'; } else { $title = $_POST['my_title']; $new_post = array( 'post_title' => $title, 'post_status' => 'publish', ...


4

You're looking for wp_delete_post. <?php $some_post_id = 1; wp_delete_post($some_post_id); The above will delete the post with ID 1 -- well, it will actually set it to a "trash" status. You can delete the post for real by setting the second parameter of wp_delete_post to true. <?php $some_post_id = 1; wp_delete_post($some_post_id, true); // really ...


4

Use some type of conditional tag to check if those posts exist or not. If they do not exist, have them created with wp_insert_post. Do this logic inside the AddMyPages function and not around the add_action function. Example You want to add a page with a parent ID only if it does not exist, and want this page to always exist. Since it's in the init hook, ...


4

you can do it using wp_insert_post, but you must specify taxonomy as well in tax_input, so it should look like $item['tax_input'] = array ( 'location' => implode(',',$location), 'sale_rental' => implode(',',$sale_rental), 'price' => implode(',',$price), ...


4

I had similar problems sometime ago with a custom CSV import, but I ended up by using some custom SQL for the bulk insert. But I hadn't seen this answer by then: Optimize post insert and delete for bulk operations? to use wp_defer_term_counting() to enable or disable term counting. Also if you check out the source for the WordPress importer plugin, you ...


3

You get just the file names because you are not uploading the files. Uploading files using AJAX is currently not that easy. Newer browsers implement the FormData interface, but for older browsers you're stuck with some kind of flash uploader, like PlUpload for example. I suggest you use PlUpload because it's bundled with WP, and send all your data together ...


3

I did not actually test your code thoroughly, but on the first glimpse: your form input is named post_category while you're grabbing $_POST['new_cat']. Adjusting that (e.g., set both to post_category) should already do. // EDIT And why are you grabbing $_POST['category'] in line 5?


3

The short answer is, post_date and post_date_gmt will be set to the date you scheduled the post to be published - not the date you created the post -, so post_date and post_date_gmt are holding the publishing date. Update: As reply to comment: When will the wp_insert_post hook be triggered? Actually both times, so on clicking Schedule and on the ...


3

Capabilties - for guests? Your problem is that a guest doesn't have any capabilities. And when posts get processed, they need to pass certain checks. One is unfiltered_html. From Codex: Allows user to post HTML markup or even JavaScript code in pages, posts, comments and widgets. Note: Enabling this option for untrusted users may result in their ...


3

Function Reference/wp insert attachment $image_id = wp_insert_attachment( $attachment, $filename, $parent_post_id ); This function will return an image id which you can use for the featured image. update_post_meta($post_id, "_thumbnail_id", $image_id);


3

The RSS feed has all the items in a fixed order(latest to oldest). In this case you can save the date & time of the last post you created, as an option & when you read the feed again, you can check the time previously saved to know which of the posts in the feed are new & insert them, then update the time again The functions you're interested in ...


3

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


3

As per the documentation: $page['comment_status'] = 'closed'; // allowed values: 'closed' or 'open'


3

It seems like the crux of the question is this: This causes the first 'if' condition to return TRUE and it inserts a duplicate post. Check for the post_name instead. That value is normalized to lowercase and dashes by sanitize_title_with_dashes so you won't have this issue. That value is also the one that WordPress enforces as unique, and the one ...


3

This is because the first time you go round the loop $post is the current post. But the second time you go around the loop, $post has not changed. The same thing happens the 3rd, 4th, 5th, etc Because the $post variable is the current post of that page, not the post you've just saved/inserted, the loops if statement will always be true, and an infinite loop ...


3

If you don't add a post_date then WordPress fills it automatically with the current date and time. To set another date and time [ Y-m-d H:i:s ] is the right structure. An example below with your code. $postdate = date('2010-02-23 18:57:33'); $new_post = array( 'post_title' => $title, 'post_content' => $description, 'post_date' ...


2

You can't format the $_POST['date'] like this... You'll have to run the value from $_POST['date'] through something like $postdate = date( $_POST['date'] )... There's also the possibility to call get_option for the blog settings. See Option Reference in Codex.


2

If you know the ID of the author you can use wp_insert_post specifying the ID and the author ID to it. $id = $post->ID; // change this to whathever $user_id = '4'; // change this too $the_post = array(); $the_post['ID'] = $id; $the_post['post_author'] = $user_id; wp_insert_post( $the_post ); The trick is to specify the ID to update the post. See ...


2

These two might be your friends: add_action('edit_post','your_action'); add_action('pre_post_update','your_action'); See: Action Reference: Post, Page, Attachment, and Category Actions for more inspiration... PS. It depends on context but consider also using wp_update_post instead wp_insert_post.



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