n00bly
n00bly

Reputation: 395

Wordpress - Get posts with specific ACF value

I have a website that's making use of the FacetWP plugin and Advanced Custom Fields for making extra fields for the posts.

Each post can have a "Department" value and I need to make a foreach for the different departments like this:

if ($department == "Office") {
    get all the post with the Value Office from the ACF field "department."
}

The custom field is a select/dropdown field.

I also need to do this same thing for other departments like "Production", "Boss", "Drinks". But I need to give the departments a title with the results below it.

What is the best way to do this?

Regards,

Update:

What I need to have in the end is this on one page in an overview:

Title: Office - Employee 1 - Employee 2 - Employee 3 - Employee 4

Title: Production - Employee 1 - Employee 2 - Employee 3

Title: Boss - Employee 1

Upvotes: 0

Views: 3753

Answers (1)

onejeet
onejeet

Reputation: 1191

The custom field ‘ location’ in this case could be a text field, radio button or select field (something that saves a single text value).

<?php 

 $location = get_field('location');

// args
$args = array(
    'posts_per_page'    => -1,
    'post_type'     => 'event',
    'meta_key'      => 'location',
    'meta_value'    => $location
);


// query
$the_query = new WP_Query( $args );

?>
<?php if( $the_query->have_posts() ): ?>
     <h1> <?php echo $location ?> </h1>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <img src="<?php the_field('event_thumbnail'); ?>" />
                <?php the_title(); ?>
            </a>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Upvotes: 3

Related Questions