Advanced custom field filtering by value in wordpress

I’m using wordpress and advanced custom fields to populate some a team section. However I use the same team section in different places and want to filter it depending on the value of a field. Instead of the user having to re enter the data on each page.

I’m trying to make it so that in the admin panel if the custom field ‘category’ has the value of ‘copyclearance’ then it only shows only the posts that have that value and hides everything else.

Below is my PHP. It prints the data out but it does not filter by the category.

<?php
        // Start the Loop.
        while ( have_posts() ) : the_post();
        endwhile;
    ?>
    <?php
    $your_query = new WP_Query( 'pagename=our-people/the-team' );
    // "loop" through query (even though it's just one page) 
    while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
        <?php 
        // args
        $args = array(
            'meta_query' => array(
                array(
                    'meta_key' => 'category',
                    'meta_value' => 'copyclearance'
                )
            )
        );
        // get results
        $the_query = new WP_Query( $args );
        // The Loop
        ?>
        <?php if( $the_query->have_posts() ): ?>
            <?php while(has_sub_field('team_profile')): ?>
            <li class="col-lg-2 teamProfile">
                <h2><?php the_sub_field('profile_name'); ?></h2>
                <p class="jobTitle"><?php the_sub_field('category'); ?></p>
            </li>
            <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_query(); //Restore global post data stomped by the_post(). ?>
    <?php endwhile;
    // reset post data (important!)
    wp_reset_postdata();
    ?>

Read more here: Advanced custom field filtering by value in wordpress

Leave a Reply

Your email address will not be published. Required fields are marked *