Support

Account

Home Forums General Issues Get posts from checked values (checkbox) does not work

Solved

Get posts from checked values (checkbox) does not work

  • Hi,

    I´m trying to get posts from a selected checkbox. I tried the following:

    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'opdracht',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'niveau_jaren',
    			'value' => '%Jaar 1%',
    			'compare' => 'LIKE'			
    		),
    		array(
    		'key' => 'field_51c82dd30047c',
    		'value' => '%Jaar 2%',
    		'compare' => 'LIKE'	
    	)
    )
    );
     
    // 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();

    This does not show anything. When I try the code for a radio button (from the documentation) it works. So what am I missing here?

    Thanks

  • @LeffDesign

    Your loop code is fine, I’ve tested it using the checkbox field inside a post. What is wrong is your ‘$args’ argument variable.

    First off, make sure the post type is correct! Secondly, the meta_key is the FIELD_NAME. make sure that is correct as well, from the looks of it, your second key name was off. Also, value => is wrong as well. don’t use %% unless that’s what the value of your checkbox is. Insert the value of your checkbox.

    It would also help if you posted a screenshot of your field group for this particular checkbox field.

    <?php $args = array(
    	'numberposts' => -1,
    	'post_type' => 'opdracht',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'FIELD_NAME',
    			'value' => 'CHECKBOX 1 VALUE',
    			'compare' => 'LIKE'			
    		),
    		array(
    		'key' => 'FIELD_NAME',
    		'value' => 'CHECKBOX 2 VALUE',
    		'compare' => 'LIKE'	
    	)
    )
    );
     
    // 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(); ?>
  • Thank you, it was indeed the % % at the values.

  • @LeffDesign

    No problem, glad you got it working! cheers

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

The topic ‘Get posts from checked values (checkbox) does not work’ is closed to new replies.