Support

Account

Home Forums Front-end Issues Filter posts by ACF value

Solved

Filter posts by ACF value

  • Hi,

    I’m trying to filter the posts by a value. I tried the following:

    <?php  if (is_page( 'testpagina' ) ) {
    // args
    
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'opdracht',
    	'meta_key' => 'niveau_jaren',
    	'meta_value' => 'jaar 2'
    );
     
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
     
    <?php wp_reset_query(); 
    
    }  ?>

    But it does not show anything. The value is correct and there are posts with this value ‘Jaar 2’. Any idea what is wrong? Thanks!

  • Well.. it does work with a radio button field group. But in my case I need to get it worked with checkboxes. Do I need to change anything?

  • Also tried this:

    <?php  if (is_page( 'testpagina' ) ) {
    // args
    
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'any',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'niveau_jaren',
    			'value' => '%Jaar 1%',
    			'compare' => 'LIKE'
    		),
    		array(
    			'key' => 'niveau_jaren',
    			'value' => '%Jaar 3%',
    			'compare' => 'LIKE'
    		)
    	)
    );
     
    // get results
    $the_query = new WP_Query( $args );

    This should mean it gets the posts with value ‘Jaar 1’ or ‘Jaar 3’, right? I don’t get anything..

  • Hi @leffdesign

    Your second lot of code looks like it should work. You have followed the example code from ACF, however, I think the ‘post_type’ => ‘any’ is your issue. I don’t think this is possible. Can you confirm the docs about this?

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

You must be logged in to reply to this topic.