Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 按ACF字段查询职位不起作用_Wordpress_Advanced Custom Fields - Fatal编程技术网

Wordpress 按ACF字段查询职位不起作用

Wordpress 按ACF字段查询职位不起作用,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,有很多与我相关的话题,但我仍然没有找到解决方案。我试图通过ACF字段(单选按钮)查询帖子,但似乎meta_查询被完全忽略了。它返回所有帖子,而不仅仅是那些符合条件的帖子。我尝试使用字段键代替字段名、其他比较等。似乎没有任何效果。希望你有一个什么可能是错误的想法!这是我的密码: <?php $post_args = array( 'post_type' => 'products', 'posts_per_page' => - 1

有很多与我相关的话题,但我仍然没有找到解决方案。我试图通过ACF字段(单选按钮)查询帖子,但似乎meta_查询被完全忽略了。它返回所有帖子,而不仅仅是那些符合条件的帖子。我尝试使用字段键代替字段名、其他比较等。似乎没有任何效果。希望你有一个什么可能是错误的想法!这是我的密码:

<?php

    $post_args = array(
        'post_type'      => 'products',
        'posts_per_page' => - 1,
        'status'         => 'publish',
        'meta_query'     => array(
            'relation' => 'AND',
            array(
                'meta_key'   => 'product_taste',
                'meta_value' => array( 'cold' ),
                'compare'    => 'IN',
            ),
            array(
                'meta_key'   => 'product_served',
                'meta_value' => array( 'grated' ),
                'compare'    => 'IN'
            )

        ),
    );
    $query     = new WP_Query( $post_args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) : ?>
            <?php
            $query->the_post();
            ?>

            <h5>
                <?php the_title(); ?>
            </h5>

        <?php endwhile ?>
        <?php wp_reset_postdata();
    }
    ?>

  • 在“元查询”数组中使用“键”和“值”
  • 在元查询中不需要使用
    meta\u键
    meta\u值
    。。。您只能直接在$args数组中使用它们。如果要添加元查询数组,只需使用
    ,例如

    $post_args = array(
        [...]
        'meta_query'     => array(
            array(
                'key'      => 'product_taste',
                'value'    => 'cold',
                'compare'  => 'LIKE',
            ),
        [...]
    

    2. 使用值数组查询序列化数据

    当您试图查询ACF数据时,使用“中的”比较“=>”和值数组也可能会出现问题,因为ACF数据可以在数据库中序列化(例如,如果数据在中继器中)

    由于您将只搜索单个值,因此可以使用
    LIKE
    而不是
    中的

    把这些放在一起

    $post_args = array(
        'post_type'      => 'products',
        'posts_per_page' => - 1,
        'status'         => 'publish',
        'meta_query'     => array(
            'relation' => 'AND',
            array(
                'key'   => 'product_taste',
                'value' => 'cold',
                'compare'    => 'LIKE',
            ),
            array(
                'key'   => 'product_served',
                'value' => 'grated',
                'compare'    => 'LIKE'
            )
        ),
    );
    $query     = new WP_Query( $post_args );
    

    如果数据是序列化的,并且您的值可能会返回多个匹配项(例如,
    如'cold'
    将匹配“cold”、“colder”、“coldest”等词),那么请尝试在值的末尾添加分号(
    ),例如

        [...]
            array(
                'key'   => 'product_taste',
                'value' => 'cold;', // add ; to the end of the value
                'compare'    => 'LIKE',
            ),
            array(
                'key'   => 'product_served',
                'value' => 'grated;', // add ; to the end of the value
                'compare'    => 'LIKE'
            )
        [...]
    

    当值在数据库中序列化时,这将起作用,因为每个项都将用分号分隔。

    如果只使用一个元查询数组,如果只包含产品元查询,这是否起作用?它只在我不使用元查询数组,而只是声明元键、元值和比较时起作用。所以它只在meta_查询之外工作。但是我需要“和”关系。在
    中使用数组和
    查询ACF数据时存在问题。根据你需要做什么,有几种方法可以解决这个问题。您是否只需要为每个meta_键搜索一个值(例如,只需为“product_taste”搜索“cold”)?是的,因为它是一个单选按钮:)很好!这让它变得更容易:)给我一分钟时间,让我为一些应该有效的东西整理出一个答案……没有效果:|就像元查询完全无效一样ignored@ValeriaMelinte等等!我想我刚刚意识到这个问题!在我说确定之前给我一点时间确认一下……哦,不,等等!因此,如果我使用不带分号的“key”和“value”,它就可以工作了!谢谢!很高兴它起作用了!是的,如果数据在数据库中序列化,则只需要分号。ACF可以根据字段的类型序列化数据。我将更新答案以突出显示,如果分号已序列化,则只需要分号,以防其他任何人也有相同的问题:)