Move images from a custom field to post_images

This topic contains 19 replies, has 3 voices, and was last updated by  Kiran 4 years, 7 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #541559

    Kiran
    Moderator
    Post count: 7069

    Try to change order of images(via move cursor) and then re-save listing.

    Kiran

    #541563

    vivek
    Full Member
    Post count: 246

    Actually, i have only a single image, max = 1 for post_images. Saving did not work…and saving all the posts will be quite a task

    So then will it work to import the URL as i was trying earlier as suggested by Alex, but that too was not updated?

    Or update featured_image in DB?

    #541574

    Kiran
    Moderator
    Post count: 7069

    Hi Vivek,

    Use following function to set featured image for post. This function will set featured image for single post. Use this function for all posts you want to set featured image.

    
    
    /**
     * Re-save post images to set featured image.
     */
    function gd_snippet_resave_post_images( $post_id ) {
    	$featured_image = geodir_get_post_meta( $post_id, 'featured_image', true );
    
    	if ( ! empty( $featured_image ) ) {
    		return true; // Don't generate featured image.
    	}
    
    	$post_images = GeoDir_Media::get_field_edit_string( $post_id, 'post_images' );
    
    	if ( ! empty( $post_images ) ) {
    		$post_images = $post_images . ' '; // Force re-save.
    
    		$featured_image = GeoDir_Post_Data::save_files( $post_id, $post_images, 'post_images', false, false );
    
    		if ( ! empty( $featured_image ) ) {
    			geodir_save_post_meta( $post_id, 'featured_image', $featured_image );
    		}
    	}
    }

    Regards,
    Kiran

    #541581

    vivek
    Full Member
    Post count: 246

    Thanks, Kiran – but can you please also tell me where i have to put this function? and where do i need to put the post id?

    #541604

    Kiran
    Moderator
    Post count: 7069

    Hi,

    1) Add following PHP snippet & above PHP snippet.
    2) Go to http://YOUR-SITE.COM/?_gd_go_set_featured_image=1

    
    
    function gd_snippet_200421_init() {
    	global $wpdb;
    
    	if ( empty( $_GET['_gd_go_set_featured_image'] ) ) {
    		return;
    	}
    
    	$post_types = geodir_get_posttypes();
    
    	foreach ( $post_types as $post_type ) {
    		$table = geodir_db_cpt_table( $post_type );
    
    		$results = $wpdb->get_results( "SELECT post_id FROM {$table} WHERE ( post_status = 'publish' OR post_status = 'pending' ) AND featured_image IS NULL OR featured_image = '' ORDER BY post_id ASC" );
    	
    		if ( ! empty( $results ) ) {
    			foreach ( $results as $row ) {
    				gd_snippet_resave_post_images( $row->post_id );
    			}
    		}
    	}
    
    	echo 'DONE :-)';
    	exit;
    }
    add_action( 'init', 'gd_snippet_200421_init', 20 );

    Regards,
    Kiran

Viewing 5 posts - 16 through 20 (of 20 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount