PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Query custom posts filtered by ACF users id

 February 05, 2022     advanced-custom-fields, wordpress     No comments   

Issue

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?


Solution

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.



Answered By - Sunday Lalbiaknia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing