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.

        // Start the Loop.
        while ( have_posts() ) : the_post();
    $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(); ?>
        // args
        $args = array(
            'meta_query' => 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>
            <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_query(); //Restore global post data stomped by the_post(). ?>
    <?php endwhile;
    // reset post data (important!)

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


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: