WordPress query posts by ACF

余生颓废 提交于 2019-12-05 23:16:14

UPDATE - Solved:

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";}

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;

}

See the end of this page: https://www.advancedcustomfields.com/resources/querying-relationship-fields/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!