0

For some odd reason custom fields keep randomly stop working - they're working one minute, then not the next and i do not understand why. The post, meta names and values are all still active - nothing has changed at all.

Any idea what the issue may be? The code i'm using is below and is contained within the header.php file - to re-iterate, it was working fine, then randomly stopped working - this happens pretty much every time i use custom fields.

    <div class="slide-title">
        <?php echo get_post_meta( get_the_ID(), 'slide_1_title', true ); ?>
    </div>
    <div class="slide-content">
        <?php echo get_post_meta( get_the_ID(), 'slide_1_content', true ); ?>
    </div>
    <div class="slide-headline">
        <?php echo get_post_meta( get_the_ID(), 'slide_1_headline', true ); ?>
    </div>
        <a href="#"><img src="/images/slides/slide1.jpg"></a>
  • 1) It isn't random, and 2) there is not enough enough information in the question to guess at an answer. Maybe your are getting server errors (timeouts?) or bad code is breaking the save. Can you provide debugging information? – s_ha_dum Jul 11 '13 at 22:36
  • There is no information being displayed when i use define('WP_DEBUG', true); - there's no extra information i can give; they just stop working - it may have been when the post was saved again. – Nikki Mather Jul 11 '13 at 22:39
  • I am sorry. I don't know what to tell you. Nothing in what you have posted should break anything. That is all display. It doesn't manipulate data in the database. Ask your host for server logs and setup WP_DEBUG to use a log file. As soon as you notice a problem look at the file. – s_ha_dum Jul 11 '13 at 22:42
  • Is this code inside Loop? That is, inside have_posts()? Post the code in more context please. – s_ha_dum Jul 11 '13 at 22:49
  • No, it's not inside Loop. The code is simply posted within another div, which holds slides for a slideshow. – Nikki Mather Jul 11 '13 at 22:52
0

get_the_ID() is a post tag. It is meant to be used inside a Loop. Per a comment above you are using it outside the Loop.

No, it's not inside Loop. The code is simply posted within another div, which holds slides for a slideshow.

get_the_ID like most post tags depends on a global variable called $post. If that variable is not populated, or is set to the wrong post, you get odd results. I think that is why you think this works 'randomly'.

A quirk of WordPress functioning is that the $post object is set well before the theme templates load, but only for some pages, which means for some pages some functions will work even when used incorrectly outside a Loop.

I think you probably want to be using get_queried_object to get the ID rather than depending on get_the_ID() but there are caveats with that too and the context you are using this in is still not clear.

$pobj = get_queried_object();
$id = $pobj->ID;

Related:

https://wordpress.stackexchange.com/a/98138/21376
https://wordpress.stackexchange.com/a/99545/21376
https://wordpress.stackexchange.com/a/91254/21376

  • I tried <?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'custom-field-name', true); wp_reset_query(); ?> however that didn't work either. Apparently this is the way to properly call a custom field outside of the loop. – Nikki Mather Jul 11 '13 at 23:35
  • It may also be worth noting that the method of calling the custom fields is working on this page; client.n8geeks.com/commentary - it uses the same method for finding the URL of the download link - works fine when the code is added through a function and therefore shortcode. – Nikki Mather Jul 11 '13 at 23:45
  • Like I said, that will work on some pages due to a peculiarity in WordPress, and something executing in a shortcode executes inside a Loop (normally). – s_ha_dum Jul 12 '13 at 0:19

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.