0

I have used the following bit of code in another script on the same site, & it worked there.

$this_post_id = get_the_ID();
$key_2_value =  get_post_meta( $this_post_id, 'custom_select', true );
if( ! empty( $key_2_value )) {
echo $key_2_value ;
 ;}
var_dump($key_2_value)

var_dump outputs string '' (length=0)

I am trying to use it in a script called events-list.php. Events-list.php provides a short list of the events (which are posts) .

The meta field is definitely called custom_select

  • 1
    Is get_the_ID returning anything? Echo it out and make sure there's actually an ID. – Matthew Campbell Dec 9 '14 at 18:17
  • 1
    You have two semicolons inside your if statement. Aside from that: What does get_the_ID() return and in what context are you calling that? Please add answers with an edit to your question. – kaiser Dec 9 '14 at 18:29
1

The variable that get_post_meta was stored in was empty because I wasn't getting an id. I was trying to use this code on a category page, not a single event. Without an id it couldn't identify the post_meta to use. So I corrected it with the following:

$latest_cpt = get_posts("post_type=event&numberposts=1"); 
    $theidone =$latest_cpt[0]->ID;

    $this_post_id =  $theidone; //get_the_ID();
    $key_2_value =  get_post_meta( $this_post_id, 'custom_select', true );
    if( ! empty( $key_2_value )) {
    $thisisworking =   $key_2_value ;

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.