Support

Account

Home Forums Front-end Issues Can't figure out how to make query checkbox filter

Unread

Can't figure out how to make query checkbox filter

  • Hello,

    I’m stuck on the following: I’d like to create a filter with the checkbox which I’m using as “category”. I named it ‘trainingscategory’. With this I’d like to filter the posts from my CPT ‘training’. I don’t suppose I’m the first one trying this but I have a hard time finding answers or perhaps I just don’t understand them. I’ve read the query and filter by custom field posts a dozen times but I can’string them together.

    In my functions.php I’ve set:

    <?php
    function my_pre_get_posts( $query ) {
    	// do not modify queries in the admin
    	if( is_admin() ) {	
    		return $query;
    	}
    	
    	// only modify queries for 'training' post type
    	if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'training' ) {
    		// allow the url to alter the query
    		if( isset($_GET['trainingscategory']) ) {
        		$query->set('meta_key', 'trainingscategory');
    			$query->set('meta_value', $_GET['trainingscategory']);
        	} 
    	}
    	// return
    	return $query;
    }
    add_action('pre_get_posts', 'my_pre_get_posts');

    For now on my page I’m only showing a table with the posts in CPT ‘training’ without the filtering.

    For my sidebar I’d like to show the values set in ‘trainingscategory’.

    <?php
    
    // Load field settings and values.
    $field = get_field_object('trainingscategory');
    $cats = $field['value'];
    
    // Display labels.
    if( $cats ): ?>
    <ul>
        <?php foreach( $cats as $cat ): ?>
            <li><?php echo $field['choices'][ $cat ]; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    This doesn’t show anything since this only shows what is set on the current page if I understood correctly. However I couldn’t figure out how to show all values in a list.

    For the filtering I think I’d need to set the above values in a add_query in the URL?
    But then how do I make the table show only the posts with that value?

    I think it should be something like below but I can’t figur out how to make this work as filter result. (I’m using a ul here instead of my table to save space)

    // Query
    		$the_query = new WP_Query(array(
    			'post_type'			=> 'training',
    			'posts_per_page'	=> -1,
    			'meta_key'			=> 'trainingscategory',
    			'orderby'			=> 'meta_value',
    			'order'				=> 'DESC'
    		));
    
    		?>
    		<?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();	 // Restore global post data stomped by the_post(). ?>

    I have a feeling I’ve read the answer a dozen times in the posts but I simply can’t figure it out. If someone could help me in the right direction I’d appreciate that alot.

    Kind regards,

Viewing 1 post (of 1 total)

The topic ‘Can't figure out how to make query checkbox filter’ is closed to new replies.