ACF的WordPress查询帖子 [英] WordPress query posts by ACF

查看:154
本文介绍了ACF的WordPress查询帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新-已解决

好的,所以我有三种自定义帖子类型:课程,模块和结果。
这些帖子类型由许多ACF(高级自定义字段)设置。

Okay so I have three custom post types 'courses', 'modules' and 'results'. These post types are set up with a number of ACF (Advanced Custom Fields).

我正在尝试根据其结果 CPT查询AFP使用'meta_query'。

I'm trying to query the 'results' CPT based on their ACF's using 'meta_query'.

我要查询的两个ACF字段是模块和用户。

The two ACF fields I'm trying to query with are 'module' and 'user'.


  • 模块-设置为关系字段类型,过滤
    模块自定义帖子类型。

  • '用户'-设置为(关系)'用户'字段类型。

我似乎只能得到一个空数组。我在 https:/上查看了示例/www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field%20parameters 仍然没有运气。

I seem to only get an empty array back. I've looked at the examples on https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field%20parameters still without any luck.

function check_results_for_user( $module_id, $user_id ){

  $posts = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'results',
    'meta_query'    => array(
            'relation'      => 'AND',
            array(
                'key'       => 'module',
                'value'     => $module_id,
                'compare'   => '=',
            ),
            array(
                'key'       => 'user',
                'value'     => $user_id,
                'compare'   => '=',
            ),
    ),
  ));

  return $posts;

}

调用函数:

print_r(check_results_for_user($post->ID, $currentUserID ));

结果:

Array ( ) 

更新-解决:

所以设法弄清楚了。是由于模块关系字段将其数据保存为序列化数组: a:1:{i:0; s:1: 9;}

Okay so managed to figure it out. Was due to the 'module' relationship field saving it’s data as a serialized array: a:1:{i:0;s:1:"9";}

因此,您必须将比较值更改为 LIKE,然后将值包装在

So you have to change the compare to 'LIKE' and wrap the value in "

function check_results_for_user( $module_id, $user_id ){

  $posts = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'results',
    'meta_query'    => array(
            'relation'      => 'AND',
            array(
                'key'       => 'module',
                'value'     => '"'.$module_id.'"',
                'compare'   => 'LIKE',
            ),
            array(
                'key'       => 'user',
                'value'     => $user_id,
                'compare'   => '=',
            ),
    ),
  ));

  return $posts;

}

请参阅本页结尾: https://www.advancedcustomfields.com/resources/querying-relationship-fields/

推荐答案

更新-解决了:

好,所以设法弄清楚了。是由于模块关系字段将其数据保存为序列化数组: a:1:{i:0; s:1: 9;}

Okay so managed to figure it out. Was due to the 'module' relationship field saving it’s data as a serialized array: a:1:{i:0;s:1:"9";}

因此,您必须将比较值更改为 LIKE,然后将值包装在

So you have to change the compare to 'LIKE' and wrap the value in "

function check_results_for_user( $module_id, $user_id ){

  $posts = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'results',
    'meta_query'    => array(
            'relation'      => 'AND',
            array(
                'key'       => 'module',
                'value'     => '"'.$module_id.'"',
                'compare'   => 'LIKE',
            ),
            array(
                'key'       => 'user',
                'value'     => $user_id,
                'compare'   => '=',
            ),
    ),
  ));

  return $posts;

}

请参阅本页结尾: https://www.advancedcustomfields.com/resources/querying-relationship-fields/

这篇关于ACF的WordPress查询帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆