Home Forums General Issues Help with query posts filtered by multiple field values


Help with query posts filtered by multiple field values

  • I have the following:

    custom post type: q-and-a
    custom taxonomy: q_and_a_category
    taxonomy terms: design, engineering, project-management

    I’m publishing three separate pages that show the custom post types by their terms. i.e., one page for Design, one for Engineering, and another for Project Management.

    To accomplish this I’m trying to create a single page template where the taxonomy term is inputted via a “select” menu.

    I tried implementing the tutorial for this, example 3:

    Like this:

    // args
    $args = array(
    'numberposts' => -1,
    'post_type' => 'q-and-a',
    'meta_query' => array(
        'relation' => 'OR',
            'key' => 'q_and_a_category',
            'value' => '%design%',
            'compare' => 'LIKE'
            'key' => 'q_and_a_category',
            'value' => '%engineering%',
            'compare' => 'LIKE'
            'key' => 'q_and_a_category',
            'value' => '%project-management%',
            'compare' => 'LIKE'
        // get results
    $the_query = new WP_Query( $args );
    // The Loop
    <?php if( $the_query->have_posts() ): ?>
    <?php the_title(); ?>
    <?php endif; ?>
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

    The page loads on the front end but no posts are displayed.

    Any suggestions? Thanks in advance.

  • Hi!

    What you’re trying to do actually already exists in WP. Since you’ve created a custom post type with a custom taxonomy there’s an archive (provided that you have set archive to true when creating the post type). So you should be able to reach each taxonomys posts by having an url something like this:

    Wordpress will use archive.php per default to display these but if you require customization and dont want to affect the regular archives you can create a new template:


    If you want specific templates for the terms you can do:



    If you still want to do it in the way you’ve done so far (i would not recommend it) you need to replace the meta-query with a tax-query since the taxonomys terms are not meta values..

    Read more here:

  • @Jonathan Thanks for the detailed response! I do know about WP’s built-in archive functionality, so I should have mentioned that I want my client to be able to edit content on these pages. That’s why I’m taking the page template approach.

    I also should have been clearer in my description above, and said that my custom field is also named “q_and_a_category”. Hence this:

    ‘key’ => ‘q_and_a_category’

    For clarity (and to avoid any potential conflicts) I have changed the custom field name to “q_and_a_category_select”.

    I tried following your advice of changing meta-query to tax-query, and applying what I read here:

    I couldn’t get that to display any posts either, and it seems like it would also remove the acf functionality that I’m looking for in the first place…specifically, the ability for my client to manually select which category of post is being displayed on the page.

    Sorry if I’m not understanding you and/or not being clear myself. Looking forward to any additional thoughts, thanks!

  • Ah okay..

    well I’m not unfamiliar to that approach to be honest since I’ve been in the same seat.. Altho you know you could add the custom content to the terms edit pages in admin and display them in the wp archive. But I guess that’s not too user friendly and especially if you have a lot of info to be put in.

    It’s a good idea to name the custom field differently than the taxonomy since it can cause issues.. so thats good!

    May I ask then how the user selects which terms the post should be connected to? Are you using acf taxonomy field?

  • Ok cool, I’m glad I’m making sense!

    I’m using an acf select field, configured like this:

    Field Name: q_and_a_category_select
    Field Type: Select

    design : Design
    engineering : Engineering
    project-management : Project Management

    To fill you in on the history, originally I had three separate page templates, each displaying posts from a specific term. For example, I used this for the Design page:

    <?php $args=array(
    	'post_type' => 'q-and-a',
    	'q_and_a_category' => 'design' //the slug for the custom taxonomy term
    	$my_query = null;
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    		while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		<?php the_title(); ?>
    	<?php endwhile; }?>
    <?php wp_reset_query(); ?>

    This works great to show just the q-and-a custom posts assigned the term “design”, but I didn’t want to have separate templates for each one.

    So the first thing I tried was this:

    ‘q_and_a_category’ => ‘<?php the_field(“q_and_a_category_select”); ?>’

    I figured it would simply just plug in the proper term and I’d be good to go. But it didn’t work and I’m not skilled enough in php to know why not.

    So I did some searching and found this acf tutorial:

    Seems like example 3 should do it, but no luck.

    Thanks again!

  • ahhhh!

    Actually your code should work but you need to change it to this:

    $selected = get_field('q_and_a_category_select');
    	'post_type' => 'q-and-a',
    	'q_and_a_category' => "$selected" //the slug for the custom taxonomy term
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    		while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		<?php the_title(); ?>
    	<?php endwhile; }?>
    <?php wp_reset_query(); ?>

    you could put the get_field directly in the array but I think this looks more structured 🙂

  • That did it!

    I guess I should have been clearer from the start, but I thought my initial approach was totally off base so I didn’t even mention it.

    Thanks so much Jonathan! I really really appreciate it.

  • Great! You learn something new everyday when coding, that’s what makes it fun 😉

    No problem Dadra, glad to help out!

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

The topic ‘Help with query posts filtered by multiple field values’ is closed to new replies.