WordPress query posts by ACF

2019-05-11 09:10发布

UPDATE - SOLVED

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

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

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

  • 'module' - Set up as a 'Relationship' field type filtering the 'module' custom post type.
  • 'user' - Set up as a (Relational) 'User' field type.

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;

}

Call function:

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

Result:

Array ( ) 

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/

1条回答
smile是对你的礼貌
2楼-- · 2019-05-11 09:50

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/

查看更多
登录 后发表回答