Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

Reply To: [Assigned] How to get the first field value from a reordered repeating field from back-end

$
0
0

Dear Benny,
Thanks for the clarification. Yes this is possible with using WP API functions alone. Below is the function I wrote for getting the first field value of a reordered repeating field. This is working well for me (add this to your theme functions.php):

//Function to return the first field value
function return_first_field_value_shortcode_func() {
	//Get post ID first
	global $post;
	if (isset($post->ID)) {
		$postid=$post->ID;
	
		//Get order of repeating fields
		$order_repeating_fields_implemented=get_post_meta($postid,'_wpcf-test-repeating-image-sort-order');		
		
		if ((is_array($order_repeating_fields_implemented)) && (!(empty($order_repeating_fields_implemented)))) {
		
			//Get the meta id of the first field value set in the order
			$field_field_value_set=$order_repeating_fields_implemented[0][0];
			
			//Get the equivalent meta value from this meta_id
			$raw_meta_value= get_meta_value_by_meta_id($field_field_value_set);
			
			//Now return this meta value to the shortcode
			if 	($raw_meta_value) {
				
				return $raw_meta_value;
			} 		
		}
	}	
}
add_shortcode('return_first_field_value_shortcode','return_first_field_value_shortcode_func');

function get_meta_value_by_meta_id( $meta_id ) { 
	global $wpdb;
	$meta_value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_id = %s", $meta_id) );
	return $meta_value;
}

To implement in your content template or post editor, simply add the shortcode: [return_first_field_value_shortcode]
Take note it returns the raw value of the image URL. You can then form the image as:

<img src="[return_first_field_value_shortcode]" />

If you study the code above, the reordering sequence of repeating fields is stored in the _wpcf-test-repeating-image-sort-order custom field (this is automatically added by Types plugin when using repeating fields). Take note that this field sequence value is only updated when also the post is updated (by clicking the update button). Please let me know how it goes. Thanks.

Cheers,
Emerson


Viewing all articles
Browse latest Browse all 20145

Trending Articles