2

Here's another conundrum that I'm toying with. There's a routine within the sitewide tags pages plugin which takes a list of custom fields (entered in the admin settings) and then uses these as keys to read a post and output the keys and values in an array. I have not been able to see it working and therefore I am trying to determine why.

Here's the routine within sitewide-tags.php: http://wordpress.org/extend/plugins/wordpress-mu-sitewide-tags/

*** line 369 in Version: 0.4.1.1 of sitewide-tags.php ***

$meta_keys = apply_filters( 'sitewide_tags_meta_keys', get_sitewide_tags_option( 'tags_blog_postmeta', array() ) );
    $global_meta['DEBUG TEST1'] = $meta_keys;
    $global_meta['DEBUG TEST2'] = $post_id;
if( is_array( $meta_keys ) && !empty( $meta_keys ) ) {
    foreach( $meta_keys as $key )
            $global_meta['DEBUG TEST3'] = 'custom loop';

        $global_meta[$key] = get_post_meta( $post->ID, $key, true );
}
unset( $meta_keys );

I have added a small amount of markup to reveal what is happening at each step.

For my test case, post_id #1121, there is one custom field which sits in the wp_9_postmeta table has one meta_key image with a corresponding meta_value http://newsite.com/files/2011/05/alfaP.jpg

However the routine returns no value within the function.

Here's what each of the debug tests shows;

DEBUG TEST1:

a:7:{i:0;s:11:"video-embed";i:1;s:9:"image-url";i:2;s:8:"link-url";i:3;s:10:"quote-copy";i:4;s:12:"quote-author";i:5;s:9:"quote-url";i:6;s:9:"audio-url";}

DEBUG TEST2: 1121
DEBUG TEST3: Custom Loop

So the first test (#1) shows that all the custom fields are being returned from the 'get_sitewide_tags_option' on line 1. The second test (#2) confirms it's looking at the correct post_id and the third test (#3) that it reaches the foreach loop and therefore passes the conditional clause.

EDIT: One further test is to check within the foreach loop and see what value is contained in $key - what I found is that $key contains the meta_key names (image, video-embed...) that should (in theory) be successful when used to retrieve the associated value using 'get_post_meta'.

I'd love to know why it doesn't - any ideas or suggestions?

3
  • Btw, I have just tried adding 'switch_to_blog' prior to the 'get_post_meta' command, but there's no change in the outcome.
    – DoctorDR
    May 25, 2011 at 11:50
  • 1
    what does $global_meta['image'] give you?
    – Bainternet
    May 25, 2011 at 15:13
  • Add global $post; Dec 17, 2011 at 18:44

1 Answer 1

2

I just solved a similar problem on a site I'm working on -- I couldn't get any meta values out of posts after I moved my loop code to a plugin.

Turns out that when you have a loop running from within a sitewide plugin, the way you access post meta is different.

Instead of writing this:

get_post_meta($post->ID, 'start-time', true);

you have to write this:

get_post_meta(get_the_ID(), 'start-time', true);

Note that first parameter, $post->ID changes to get_the_ID(). This fixed the problem for me, hopefully it does for you too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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