【问题标题】:Wordpress - Get posts with specific ACF valueWordpress - 获取具有特定 ACF 值的帖子
【发布时间】:2019-05-18 16:44:42
【问题描述】:

我有一个网站,它使用 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

【问题讨论】:

    标签: wordpress foreach advanced-custom-fields


    【解决方案1】:

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

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

    【讨论】:

    • 我忘了说,自定义字段是一个选择/下拉字段
    • 正如我在回答文本中提到的,这对下拉菜单也有效,但不应该是多值选择。表示您只选择一个值。没关系吗?
    • 是的,只有一个值是可能的,谢谢!我将尝试使用此代码。
    • 如果这有帮助。请接受并投票给答案。
    • 是的,对于一个特定的部门,它正在发挥作用。但我需要它用于更多部门,我已经用我需要的确切内容更新了我的答案。
    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 2022-06-29
    • 2021-09-23
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    相关资源
    最近更新 更多