Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi there,

    are you talking about changing (i.e., setting) the geo data for already existing translations, or would you like to automatically copy the location information every time you synchronize related posts?

    The former is somewhat complicated, while the latter could be achieved by using the mlp_pre_save_post_meta filter. For instance like so:

    add_filter( 'mlp_pre_save_post_meta', function ( array $post_meta ) {
    
    	$post_id = get_queried_object_id();
    
    	$meta_keys = array(
    		'_wp_geo_longitude',
    		'_wp_geo_latitude',
    	);
    
    	foreach ( $meta_keys as $meta_key ) {
    		$meta_value = get_post_meta( $post_id, $meta_key, true );
    		if ( false === $meta_value ) {
    			continue;
    		}
    
    		$post_meta[ $meta_key ] = $meta_value;
    	}
    
    	return $post_meta;
    } );

    Kind regards,
    Thorsten

    Thread Starter konus77

    (@konus77)

    Hello, thank you for your fast replay!
    Yes, I’d like to automatically copy the location information every time I synchronize related posts.

    But wait, I am not sure, what “synchronize related posts” means: Is this the action after I press the button “Quellbeitrag kopieren” (copy source post) in the MLP translation box?

    I tried the above code (in function.php and as plugin), but nothing happens. The mentioned meta_keys are still empty in the second language post after pressing that button and saving the post.

    I am sorry since I know this is not the correct place to ask, but I even don’t know how I can debug that code. How can I find out, if it is called at all?

    Post synchronization happens whenever you save a post that has related posts (i.e., it is translated).

    The above code doesn’t work because get_queried_object_id() isn’t working in WordPress admin. You just have to use get_the_ID() instead. Here’s the full code:

    add_filter( 'mlp_pre_save_post_meta', function ( array $post_meta ) {
    
    	$post_id = get_the_ID();
    
    	$meta_keys = array(
    		'_wp_geo_longitude',
    		'_wp_geo_latitude',
    	);
    
    	foreach ( $meta_keys as $meta_key ) {
    		$meta_value = get_post_meta( $post_id, $meta_key, true );
    		if ( false === $meta_value ) {
    			continue;
    		}
    
    		$post_meta[ $meta_key ] = $meta_value;
    	}
    
    	return $post_meta;
    } );

    Kind regards,
    Thorsten

    Thread Starter konus77

    (@konus77)

    You are my hero! That is working great!
    THANK YOU!

    You’re welcome. Glad it worked.

    And thanks for the review. 😉

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘copy custom fields’ is closed to new replies.