3
votes

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
in ACF choose field type relational -> user set also relationship -> 'user' - Set up as a 'Relationship' field type.Anand Choudhary
are you sure your user_id is not blank try to get user id $user_id = get_current_user_id();Anand Choudhary
Thanks for the help managed to figure it out was due to the 'module' data being saved as a serialized array - I've updated the questionTom Smith
Answer your own question when you can - So that no more people, like me, will go in and try to solve it before reading that it has been solved :)Stender

1 Answers

2
votes

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/