Support » Fixing WordPress » get_post_meta not working

  • Hi. Trying to expand my WordPress theme coding and general php skills. I’ve run into a snag with some code for displaying custom fields in a template.

    The chart I want to display has two types of fields, one for a link name, the other for the page link URL . If the link is user-unspecified, I want the page to display just the plaintext name. If the link is specified, I want the name and URL to form the link.

    ‘dad’ and ‘dad_link’ would be an example of a pair of fields, one containing a name, the other a wordpress page URL. So I feed ‘dad’ to this function:

    <?php function islink($key){
    
    $key2 = $key.'_link';
    $meta = get_post_meta($post->ID, $key2, true);
    if($meta != '')
    echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    else
    echo the_field($key);
    
    }?>

    And then.. <?php islink('dad');?> This should output a named link if the field dad_link is set, or just the dad string, if not. But I can’t get it working.

    If I use the code if($meta != '') then nothing becomes a link.
    If I use the code if($meta != 'null') then everything becomes a link, regardless of if the link is null or not. The code isnt picking between the instances where the link is set and not set.

    So there’s something I’m not understanding about get_post_meta. Anyone see what I’m missing?

Viewing 13 replies - 1 through 13 (of 13 total)
  • I guess your code lacks global $post;

    function islink($key){
    global $post; // add here
    $key2 = $key.'_link';
    $meta = get_post_meta($post->ID, $key2, true);
    if($meta != '')
    echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    else
    echo the_field($key);
    
    }

    Please check whether $post->ID is set propally. echo $post->ID; will help you.

    As it is inside a function you need to bring the $post into scope!

    <?php
    function islink($key){
    	global $post;
    	$meta = get_post_meta($post->ID, '_link', true);
    	if($meta != '') {
    		echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    	} else {
    		echo the_field($key);
    	}
    }?>

    As you are learning you could shorten it with sorthand statements
    $return_value = the_statement ? ‘where ? = then is true’ : ‘where : = else is false’;

    <?php
    function islink($key){
    	global $post;
    	$meta = get_post_meta($post->ID,'_link',true) ? '<a href="',the_field('_link'),'">',the_field('_link'),'</a>' : the_field('_link');
    	echo $meta;
    }
    }?>

    Functions are not really needed in this scenario, you could call it without the use of a function, directly in the page, page content loop, or posts content loop, templates code, where the $post is already in scope!

    <?php $meta = get_post_meta($post->ID,'_link',true) ? '<a href="',the_field('_link'),'">',the_field('_link'),'</a>' : the_field('_link'); echo $meta; ?>

    HTH

    David

    Thread Starter seancho

    (@seancho)

    Edit: posted before reading the response above…

    Weird.. When I include global $post; it is all links again — all null values for dad_link are ignored and links with a missing URL result – <a href=""> So this is the new code:

    <?php function islink($key){
    global $post;
    $key2 = $key.'_link';
    $meta = get_post_meta($post->ID, $key2, true);
    if($meta != '')
    echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    else
    echo the_field($key);
    }?>

    Strangely, the following code does work, and I don’t understand why..

    <?php
    $key = 'dad';
    $key2 = 'dad_link';
    $meta = get_post_meta($post->ID, $key2, true);
    if($meta != 'null')
    echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    else
    echo the_field($key);
    echo get_post_meta($post->ID, $key2, true);
    ?>

    I can run that code on the same page as the other, and it correctly switches between plain text and link text for the dad field while the other does not. The echo at the end returns null and 92, which is the ID of the linked page.

    Anyone understand what is going on?

    Thread Starter seancho

    (@seancho)

    OK, so responding to the 2nd suggestion. As I say above, even with global $post it still doesn’t work. And I’ll have to look up what that does.

    I’m using a function because there are 16 places I need that code. So the source code is much lighter and cleaner this way. And besides, it’s my first crack at a php function, so I can’t back down now!

    Thanks for info on global $post. But I’m still not there, yet.

    I’m using a function because there are 16 places I need that code.

    I can only think of a few places?

    The function call will only work after the call to the_post() when $post is in scope!

    The best place is inside ‘the loop’ (loop.php or content.php), when you know thw $post is already in scope.

    Alternative places index.php, archive.php, category.php, search.php and in single.php

    The function can be changed to return $meta;

    return $meta;

    Example code for twenty eleven index.php

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    
             <?php echo islink('dad'); ?>
    	<?php get_template_part( 'content', get_post_format() ); ?>
    
    <?php endwhile; ?>

    it’s my first crack at a php function, so I can’t back down now!

    Yes you can, it is good to re-evaluate, re-write, improve the code, or back down, shows you are willing to learn new or different ways to code.

    It is a learning curve, I can often write a function or code block, find another way and just “rip it up and start again!”

    HTH

    David

    Thread Starter seancho

    (@seancho)

    Well, I’m not entirely understanding all this. Scope is a new concept. Putting global $post; in the function brings $post into scope, no? But it’s still not behaving as I wish.

    Late. I will have to read up on all this tomorrow!

    Just the way I described it, sorry to confuse things but scope = include, scope is more PHP than WordPress.

    This PHP page might help you understand it more, but in short terms, $post is just a WordPress variable and to include it in a function you use the global $post; call

    In PHP global variables must be declared global inside a function if they are going to be used in that function.

    To use a WordPress or Custom variable inside a function you must include it with the word global.

    We could set our own variable as an example in the loop.php or content.php:

    <php
    $day = date( 'd',strtotime( esc_html( get_the_date() ) ) );
    $month = date( 'M',strtotime( esc_html( get_the_date() )) );
    
    function get_my_date() {
    	global $day, $month;
             return 'Posted ' .$month .' ' .$day;
    }
    ?>

    Then in the section I want to call the function.

    <?php echo get_my_date(); ?>

    This should return:
    Posted May 09

    HTH

    David

    Thread Starter seancho

    (@seancho)

    Makes sense. So in my code, I have now brought $post within the scope of the function:

    <?php function islink($key){
    global $post;
    $key2 = $key.'_link';
    $meta = get_post_meta($post->ID, $key2, true);
    if($meta != '')
    echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    else
    echo the_field($key);
    }?>
    
    <?php islink('dad');?>

    But it doesn’t work. This function returns:

    <a href="">Dad</a>

    instead of the string ‘Dad’ when the field dad is set to ‘Dad’ and the field dad_link is unset, ie null.

    Why? When dad_link is null the function should return ‘Dad’.

    Thread Starter seancho

    (@seancho)

    Well, I’ve got it working. This works:

    <?php function islink($key){
    global $post;
    $key2 = $key.'_link';
    $meta = get_post_meta($post->ID, $key2, true);
    if($meta != 'null')
    echo '<a href="',the_field($key2),'">',the_field($key),'</a>';
    else
    echo the_field($key);
    }?>

    I have to use the string ‘null’ instead of ”. I don’t understand it, but at this point I’ll take the working code. There’s something I’m not understanding about get_post_meta.

    You have a good grasp of the get_post_meta()

    The If statement as we know will return only a true or false, where ”, 0, null, or empty will return a false and ‘Hello’ or a non-zero number will return a true.

    ” or ‘null’ should both return a false so testing for a value being true might work better.

    if( $meta ) will return true if not null or empty

    You could also try isset()

    if( isset($meta) && $meta )

    HTH

    David

    My bad, the database DOES have the same ID’s so that’s one less issue. Here is the metadata after it’s imported into the DB:

    a:5:{s:10:"resolution";s:18:"2040x3800 | 300DPI";s:10:"zoomsample";s:66:"imageZOOM";s:9:"psdaction";s:80:"downloadZIP";s:7:"widthmm";s:2:"54";s:8:"heightmm";s:3:"120";}

    So the data is there but the following returns nothing:

    $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
    echo '<b>' . __('Resolution: ') . '</b>' . $my_meta['resolution'] . '<br>';

    Nothing. I can see it in the DB but PHP thinks otherwise.

    BTW, the exact same code is running fine on the live site and is happily displaying the data.

    Interesting, part 1 of my 2 part comment never appeared…

    So I have my theme which is using get_post_meta quite successfully and it’s humming along just fine. I make frequent backups and all the data is there. So I recently tried to recover a DB as a test to see how well I would recover and it turns out that for the most part it’s great. However, all my custom fields are empty — but only get_post_meta returns the values as empty. The data is still there in the SQL backup as well as in the DB (I had thought the post ID was changed but that was a brain fart).

    It’s really quite frustrating. This thread was the closest thing I could find to people talking about get_post_meta

    Please post your own topic.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘get_post_meta not working’ is closed to new replies.