Support

Account

Home Forums Front-end Issues custom field "the_field" inside the loop array Reply To: custom field "the_field" inside the loop array

  • I apologize in advance if I’m not understanding your question correctly.

    So you have a custom post type (produktark) with a checkbox field in it. Then on another page, you have a ACF to select which checkbox value from the CPT you want to query by?

    Assuming that vis_produktark_fra is the field key for the field on the page you want to display the custom posts on, then you should be able to use get_field() rather than the_field(). the_field echos the value, which you don’t want to do.

    So assuming I’m understanding you correctly, your code could look like:

    
    <?php 
    
    $vis_produktark_fra = get_field('vis_produktark_fra');
    // args
    					
    $args = array(
    
    'post_type' => 'produktark',
    'posts_per_page' => -1,
    'meta_query' => array(
               array(
                   'key'       => 'produkttyp',
                   'compare'   => 'LIKE',
                    'value'     =>  $vis_produktark_fra
                    )
    )
    );
    					 
    // get results
    $the_query = new WP_Query( $args );
    					 
    // The Loop
    ?>
    

    OR

    
    <?php 
    
    // args
    					
    $args = array(
    
    'post_type' => 'produktark',
    'posts_per_page' => -1,
    'meta_query' => array(
               array(
                   'key'       => 'produkttyp',
                   'compare'   => 'LIKE',
                    'value'     =>  get_field('vis_produktark_fra')
                    )
    )
    );
    					 
    // get results
    $the_query = new WP_Query( $args );
    					 
    // The Loop
    ?>
    

    this is basically the same thing in my first code snipped