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 didn’t quite understand you, where do you see this difference?

    Here’s the entire code I have till now:

    function change_columns( $cols ) {
      $cols = array(
        'cb'       => '<input type="checkbox" />',
    	'title'      => __( 'Title',      'trans' ),
        'number'      => __( 'Number',      'trans' ),
    	'author'      => __( 'Author',      'trans' ),
    	'Date'      => __( 'Date',      'trans' ),	
      );
      return $cols;
    }
    add_filter( "manage_edit-usa-locations_columns", "change_columns" );
    
    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);
    	
    	
        }
    }
    
    add_action('manage_usa-locations_posts_custom_column', 'columns_content_only_locations', 10, 2);
    
    // Make these columns sortable
    function sortable_columns() {
      return array(
        'number'      => 'number',
    	'title'      => 'title',
      );
    }
    
    add_filter( "manage_edit-usa-locations_sortable_columns", "sortable_columns" );
    
    function number_column_order( $vars ) {
        if ( isset( $vars['orderby'] ) && 'count' == $vars['orderby'] ) {
    
    		$vars["meta_key"] = "count";
    		$vars["order"] = "ASC";
    		$vars["orderby"] = "meta_value_num";
    	
        }
     
        return $vars;
    }
    add_filter( 'request', 'number_column_order' );