skip to Main Content

I have custom post type called journals and an ACF Users field called editors (return format User ID ).

While adding a new journal, you can select multiple users as Editors.

Now, I want to create a query to get all journals where the current user is assigned as editor.

I tried something like this:

$current_user = wp_get_current_user();

$args = array(  
  'post_type' => 'journals',
  'post_status' => 'publish',
  'meta_query' => array(
     array(
       'key' => 'editors',
       'value' => $current_user->ID, 
       'compare' => 'IN'
     )
   )
);

How can I achieve this?

2

Answers


  1. Chosen as BEST ANSWER

    I used LIKE instead of IN as below -

    $current_user = wp_get_current_user();
    
    $args = array(  
        'post_type' => 'journals',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'editors',
                'value' => $current_user->ID,
                'compare' => 'LIKE'
            )
        )
    );
    

    This works for me.


  2. Sunday Lalbiaknia is correct. But on my end there is a slight modification on this since $current_user->ID is an integer, you can make it this way (take note of the semicolon):

    $current_user = wp_get_current_user();
    
    $args = array(  
        'post_type' => 'journals',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'editors',
                'value' => $current_user->ID . ';',
                'compare' => 'LIKE'
            )
        )
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search