Support » Plugin: Pods - Custom Content Types and Fields » Array Meta Query of a Custom Field ¿How?

  • Resolved virgilio27

    (@virgilio27)


    In the posts I have created a custom field called actors. Also have created a CPT called CPTactors and I have related it to the previous custom field bidirectionally.

    Now I want that when I access the page of the CPT-actors I get a list of all the posts that actor has.

    $name = $post->post_name; //get the slug of the current CPTactors 
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 199,
    'meta_query' => array(array(
    	'key' => 'actors',
            'value' => $name,
    	'compare' => 'LIKE',
    	),
    ),
    );
    
    $loop = new WP_Query($args)
    .
    .
    .
    

    I’m trying this but nothing comes out. any ideas?

    • This topic was modified 2 years ago by virgilio27.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @virgilio27

    Please try the IN compare type.

    Cheers, Jory

    Thread Starter virgilio27

    (@virgilio27)

    Nothing comes out, any way to see what the array contains?

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @virgilio27

    Ah right, sorry, now I also see you’ve used the post name. You could use the post ID as these are stored in the database.

    Cheers, Jory

    Plugin Author Jory Hogeveen

    (@keraweb)

    Oh, and you can use -1 for posts_per_page if you want to fetch all relationships.

    Thread Starter virgilio27

    (@virgilio27)

    Nothing. The strange thing is that if I leave it like that* the posts do come out, but all the posts that have any actor come out, not only those of that CPT.

    *

    $name = $post->post_name; //get the slug of the current CPTactors 
    $args = array(
    'post_type' => 'post',
    'posts_per_page' => 199,
    'meta_query' => array(array(
    	'key' => 'actors',
    	),
    ),
    );
    
    $loop = new WP_Query($args)
    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @virgilio27

    You’ll still need to add the full meta query:

    
    	'key' => 'actors',
            'value' => $post->ID,
    	'compare' => 'IN',
    

    Cheers, Jory

    Thread Starter virgilio27

    (@virgilio27)

    Yeesss. It Works!!!

    The error was in the field “value”, wasn’t it supposed to be the exact name?

    Plugin Author Jory Hogeveen

    (@keraweb)

    No, relationships are always stored by their ID’s.

    And you’re welcome! The best way to say thanks is to leave a 5 star review at https://wordpress.org/plugins/pods/ and (if you’re feeling especially generous) become a Friend of Pods at https://friends.pods.io/

    Cheers, Jory

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Array Meta Query of a Custom Field ¿How?’ is closed to new replies.