WordPress.org

Ready to get started?Download WordPress

Forums

Meta_Key to Featured Image problem (4 posts)

  1. RandallFlagg
    Member
    Posted 1 year ago #

    Well, searching this forum I've found many answer to my problem, none of them help me =(

    I'm updating a theme and find the problema tha they didn't used the "Featured Image" for thumbnail, instead they used a Custom Field (meta_key) called "post_image" image.

    This thumbnail it seems (I really don't know) that haven't been uploaded for posts, instead it seems that they use to upload the images separete from the post, copied the link to this custom field and voilá.

    Now, what I want to do is grab this meta_key and change it to the featured image. So, for what I read in the forum, I have to run this in the mysql.

    UPDATE wp_postmeta SET meta_key = REPLACE ( meta_key, 'post_image', '_wp_attached_file' );

    But nothing happend.
    I put everything back to normal with my backup now.. but nothing. I tried the plugins Auto Featured Image and Auto Post Thumbnail. Nothing.
    Any clue?

  2. RandallFlagg
    Member
    Posted 1 year ago #

    Okay, got this part now, (but still don't know how to do it)

    Every post create a _thumbnail_id. This thumbnail_id have the meta_value with the ID that contains the image information.
    What I dont know is how to create in the database the new _thumbnail_id and relate them to each other...

  3. RandallFlagg
    Member
    Posted 1 year ago #

    This is the closest I got to a solution so far. In functions.php I create a new function.

    function meta_to_featured_image($postID, $image){
    
    	$filename = $image;
    	$wp_filetype = wp_check_filetype(basename($filename), null );
    	$wp_upload_dir = wp_upload_dir();
    	$attachment = array(
    	 //'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ),
    	 'guid' =>  $filename,
    	 'post_mime_type' => $wp_filetype['type'],
    	 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    	 'post_content' => '',
    	 'post_status' => 'inherit'
    	);
    	$attach_id = wp_insert_attachment( $attachment, $filename, $postID );
    	// you must first include the image.php file
    	// for the function wp_generate_attachment_metadata() to work
    	require_once(ABSPATH . 'wp-admin/includes/image.php');
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    	wp_update_attachment_metadata( $attach_id, $attach_data );
    	}

    Since I'm getting the file path from an actual URL, I pass that to the GUID.
    Problem is that is does not work properly. I extract the image, but It can't regenerate the thumbnails with the plugin Regenerate Thumbnails, since it finds a problem with the path in the server.

    Since I have 380 post, I do this also in the index

    query_posts(array('showposts' => 380 ));
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    $imagen = get_post_meta($post->ID, 'post_image');
    echo $post->ID.'- '.$imagen[0].'<br>';
    
    	meta_to_featured_image($post->ID, $imagen[0]);
    endwhile; endif;

    So I call all my posts.
    But still wont work for me. It generate an image per post, but wont work for Auto Featured Image or Auto Post Thumbnail to create a Featured Image.

  4. RandallFlagg
    Member
    Posted 1 year ago #

    No help? =(

Topic Closed

This topic has been closed to new replies.

About this Topic