首页 > 解决方案 > Wordpress - 获取具有特定 ACF 值的帖子

问题描述

我有一个使用 FacetWP 插件和高级自定义字段为帖子制作额外字段的网站。

每个帖子都可以有一个“部门”值,我需要为不同的部门创建一个 foreach,如下所示:

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

自定义字段是一个选择/下拉字段。

我也需要为“生产”、“老板”、“饮料”等其他部门做同样的事情。但是我需要给部门一个标题,下面有结果。

做这个的最好方式是什么?

问候,

更新:

最后我需要的是在概述的一页上:

职位:办公室 - 员工 1 - 员工 2 - 员工 3 - 员工 4

标题:生产 - 员工 1 - 员工 2 - 员工 3

职位:老板 - 员工 1

标签: wordpressforeachadvanced-custom-fields

解决方案


在这种情况下,自定义字段“位置”可以是文本字段、单选按钮或选择字段(保存单个文本值的东西)。

<?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(). ?>

推荐阅读