0

I have two CPTs that are related via a meta value on one of them. I have a meta_key in one CPT that holds the ID of its linked other CPT.

enter image description here

If I go to that post in the admin and dump its meta like this,

$meta = get_post_meta( $_GET['post'] );

dd( $meta );

It then dumps out an array as I would expect, matching what is in DB,

Array
(
    .....
    [practice_id] => Array
        (
            [0] => 4519
        )
    .....
)

So I then run my query for posts on the other CPT edit screen and dump results,

$current_practice_id = $_GET['post'];
$practice_args = array(
    'post_type'  => 'sf-location',
    'meta_query' => array(
        array(
            'key' => 'practice_id',
            'value' => $current_practice_id,
        ),
    ),
);
$practice_array = get_posts( $practice_args );

dd($practice_args);

And it just returns an empty array.

$current_practice_id is getting set to the proper Post ID, in this case, 4519.

So it should be returning the post seen in the first screenshot, Post ID 4520 in this case.

What am I missing here?

Thank you.

  • 2
    Did you account for post_status? – Nicolai Jan 31 '18 at 6:50
  • No I did not! Thank you, that was it. Wow :) – thatryan Jan 31 '18 at 7:06
  • You are welcome! It happened to me before :) Not even sure, if it's worth to make it an "official" answer. – Nicolai Jan 31 '18 at 7:10
0

As /u/Nicolai suggested in the question comment section, it's always a good idea to add in post_status not only for validity but for speed purposes too. It could look like this:

$practice_args = array(
    'post_type'     => 'sf-location',
    'post_status'   => 'publish',
    'meta_query'    => array(
        array(
            'key'   => 'practice_id',
            'value' => $current_practice_id,
        ),
    ),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.