Support

Account

Home Forums ACF PRO Custom query: Radio select with Custom fields based on selection.

Solved

Custom query: Radio select with Custom fields based on selection.

  • I have a custom post type case_study

    There are two sort of custom fields called sales_increase and case_study.

    Each of these have custom fields that appear when selected while writing a new Case Study post via the Conditional Logic option in ACF, depending on which radio option is selected, radio_sales_incease or radio_case_study

    What I want to do is create two different WP_Querys, one just for posts that have the sales_increase selected and another in the same template for full_case_study. I’ve poured over every post related to radio buttons but I just can’t figure this out.

    Here is the query I tried to write:

    $args = array(
          	'numberposts'	=> -1,
          	'post_type'		=> 'case_study',
          	'meta_key'		=> 'case_radio',
            'choices'     =>  array(
              'radio_sales_increase' => 'Sales Increase',
             ),
          );

    The only part of this that seems to work is the post_type argument.

    Here is the custom-fields code for radio selects that ACF exports if that helps:

    array (
    		'key' => 'field_5592fcb8e266f',
    		'label' => 'Case Radio',
    		'name' => 'case_radio',
    		'type' => 'radio',
    		'instructions' => 'Select Case Study post type.',
    		'required' => 1,
    		'conditional_logic' => 0,
    		'wrapper' => array (
    			'width' => '',
    			'class' => '',
    			'id' => '',
    		),
    		'choices' => array (
    			'radio_case_study' => 'Case Study',
    			'radio_sales_increase' => 'Sales Increase',
    		),
    		'other_choice' => 0,
    		'save_other_choice' => 0,
    		'default_value' => '',
    		'layout' => 'horizontal',
    	),
    	array (
    		'key' => 'field_5592fd1e644a4',
    		'label' => 'Case Study',
    		'name' => 'full_case_study',
    		'type' => 'wysiwyg',
    		'instructions' => 'Enter Case Study write up.',
    		'required' => 0,
    		'conditional_logic' => array (
    			array (
    				array (
    					'field' => 'field_5592fcb8e266f',
    					'operator' => '==',
    					'value' => 'radio_case_study',
    				),
    			),
    		),
    		'wrapper' => array (
    			'width' => '',
    			'class' => '',
    			'id' => '',
    		),
    		'default_value' => '',
    		'tabs' => 'all',
    		'toolbar' => 'full',
    		'media_upload' => 1,
    	),
    	array (
    		'key' => 'field_5592fd74f5563',
    		'label' => 'Sales Increase',
    		'name' => 'sales_increase',
    		'type' => 'number',
    		'instructions' => 'Enter the sales increase percentage number.',
    		'required' => 0,
    		'conditional_logic' => array (
    			array (
    				array (
    					'field' => 'field_5592fcb8e266f',
    					'operator' => '==',
    					'value' => 'radio_sales_increase',
    				),
    			),
    		),
    		'wrapper' => array (
    			'width' => '',
    			'class' => '',
    			'id' => '',
    		),
    		'default_value' => '',
    		'placeholder' => '',
    		'prepend' => '',
    		'append' => '',
    		'min' => '',
    		'max' => '',
    		'step' => '',
    		'readonly' => 0,
    		'disabled' => 0,
    	),
    ),
    'location' => array (
    	array (
    		array (
    			'param' => 'post_type',
    			'operator' => '==',
    			'value' => 'case_study',
    		),
    	),
    ),
  • You would do a query for posts based on the radio something like this

    
    // get posts with radio value of 'radio_case_study'
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'case_study',
        'meta_key' => 'case_radio',
        'meta_value' => 'radio_case_study'
    );
    
  • Yep, nailed it. Thanks John!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Custom query: Radio select with Custom fields based on selection.’ is closed to new replies.