Insert an image into a post by API

You can set the uploaded image as the post thumbnail via, update_post_meta($post_id,’_thumbnail_id’,$attach_id); Example… if ($_FILES) { foreach ($_FILES as $file => $array) { if ($_FILES[$file][‘error’] !== UPLOAD_ERR_OK) { return “upload error : ” . $_FILES[$file][‘error’]; } $attach_id = media_handle_upload( $file, $post_id ); } } update_post_meta($post_id,’_thumbnail_id’,$attach_id); If you are uploading an array of images, then you … Read more

Move from old custom field to new post_thumbnails

Had the same issue last week and here is what i did: if (has_post_thumbnail()) //if the post already has a post thumbnail then just use that the_post_thumbnail($size=”post-thumbnail”, $attr=””); else{ //if not then convert the custom field to the post thumbnail and display that $at_url = get_post_meta($post->ID, ‘image’, true); $at_id = get_image_id_by_url($at_url); delete_post_meta($post->ID, ‘image’); if ($at_id){ … Read more

How can I add a set featured image function to a theme that doesn’t already have it built in?

You can add this line to your functions.php file to add the post thumbnail option to all post types: add_theme_support( ‘post-thumbnails’ ); If you want it for only one post type, you pass an array of post type names as second argument: add_theme_support( ‘post-thumbnails’, array( ‘post’ ) ); #shown in posts only To display the … Read more

Skip posts without a thumbnail in loop

You can try to add ‘meta_key’ => ‘_thumbnail_id’, to your input arguments: $args = array( ‘numberposts’ => 5, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘meta_key’ => ‘_thumbnail_id’, ); to query only posts with thumbnails (i.e. featured images). ps: instead of this structure: if ( !has_post_thumbnail() ) { continue; } … Read more

admin-ajax.php returns 0. How do I debug it and fix it?

Enable Debugging WordPress has constants defined in wp-config.php where you can print errors to the screen and log them in a separate file located /wp-content/debug.log. It looks like this: define( ‘WP_DEBUG’, true ); define( ‘WP_DEBUG_DISPLAY’, true ); define( ‘WP_DEBUG_LOG’, true ); You can then print your own information to the debug log at specific point … Read more

Best way to filter featured image text for a custom post type?

Regardless if you go the PHP or jQuery route, I suggest you set up your filters or enqueue your Javascript in the admin_head-post[-new].php or admin_print_scripts-post[-new].php hook. There you can be sure that the global variable $post_type is set, and can check whether it is slide. Since the post thumbnail code is called after these hooks, … Read more

Multiple featured image thumbnails for post types (Multiple Post Thumbnails plugin)

Dude, you’re doing this the hard way. You can do what you want totally with a single ‘featured post thumbnail’. Read the documentation about thumbnail sizes in themes. You can inject custom thumbnail sizes in your themes funcitons.php and then give them as a parameter to the (get_)the_post_thumbnail() function. Please just read the documentation at … Read more