Support

Account

Home Forums ACF PRO Getting number of times selected from relationship field Reply To: Getting number of times selected from relationship field

  • I’m not sure what you’re saying is a good way to go by, seems to be too complicated for such a thing, is it really necessary? I tried something else which seems to work when I echo the meta_value from the WordPress default custom field.

    Here’s my code for this now, just added the last update_post_data line.

    function columns_content_only_locations($column_name, $post_ID) {
        if ($column_name == 'number') {
    
    	$locations = get_posts(array(
    		'post_type' => 'post',
    		'category'  => '3',
    		'numberposts'=>-1,
    		'orderby' => 'title',
    		'posts_per_page'   => -1,
    		'meta_query' => array(
    			array(
    				'key' => 'headquarters', // name of custom field
    				'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    				'compare' => 'LIKE'
    			)
    		)
    	));
    	
    	$totalCount = count( $locations );
    	echo $totalCount;
    	
    	update_post_meta($post_ID, 'count', $totalCount);
    	
    	
        }
    }

    And my sorting function is there but it sorts in a weird order I can’t figure what it is, when I sort it goes always to to the same order: 2, 6, 15, 4, 8 and so on… not ordered at all.

    Here is it:

    function number_column_order( $vars ) {
        if ( isset( $vars['orderby'] ) && 'count' == $vars['orderby'] ) {
    
    		$vars["meta_key"] = "count";
    		$vars["order"] = "ASC";
    		$vars["orderby"] = "meta_value meta_value_num";
    	
        }
     
        return $vars;
    }
    add_filter( 'request', 'number_column_order' );