0

I've created a custom post type in WordPress, and in the single post template, I'm successfully pulling in my custom data using the get_post_custom() function.

But inside The Loop on the list of posts, the function doesn't work and comes back with an empty array.

Here's what I have:

<?php $loop = new WP_Query( array( 'post_type' => 'web-design', 'posts_per_page' => 10 ) ); ?>

<?php $i = 0; ?>

<?php while ( $loop->have_posts() && $i < 3 ) { $loop->the_post(); ?>

<article class="project-link <?php echo 'num' . $i ?>">
    <div class="pad">

        <?php $project_info =  get_post_custom(); ?>

        <?php 

        foreach ($project_info as $i => $values) {
            print "$i {\n";
            foreach ($values as $key => $value) {
                print "$key => $value\n";
            }
            print "}\n";
        }

        ?>      

        <?php echo $project_info['url'][0]; ?>  

And I don't have anything coming back at all.

Does anyone know why this doesn't work? It works fine in the single post template, why not in the loop?

Thanks!

shrewdbeans
  • 11,971
  • 23
  • 69
  • 115

2 Answers2

0

the post_custom() has many variables stored in arrays ..

if you know the specific key , or value that you need you can use

or just itirate through them like this :

<?php

  $mykey_values = get_post_custom_values('my_key');
  foreach ( $mykey_values as $key => $value ) {
    echo "$key  => $value ('my_key')<br />"; 
  }

?>

or for a specific post

<?php

  $custom_fields = get_post_custom(72);
  $my_custom_field = $custom_fields['my_custom_field'];
  foreach ( $my_custom_field as $key => $value )
    echo $key . " => " . $value . "<br />";

?>
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • Thanks Obmerk, but for some reason this still doesn't work within the Loop. For some reason, it's just returning an empty string or array. It works perfectly on the single post though. – shrewdbeans Mar 10 '12 at 18:23
  • if it works on single post - than it works in a loop- the problem might be your custom query.. note also that what you have showed in your example are NESTED arrays – Obmerk Kronen Mar 10 '12 at 18:43
  • I see, thanks for explaining, how can I set about testing my custom query? I used print_r() with my $loop object, and it didn't return any of the custom values. Could the problem also be with in the set up of the custom post in the functions.php file? Or is the fact that it works in the single post show that it's okay? Thank again – shrewdbeans Mar 11 '12 at 08:27
  • like i said before - It IS returning the values, but as Arreys INSIDE and arrey . Nested arrays .. try to read here :http://stackoverflow.com/questions/26007/iterating-over-a-complex-associative-array-in-php or here :http://stackoverflow.com/questions/2207599/multidimensional-array-iteration – Obmerk Kronen Mar 11 '12 at 08:57
  • Hmmm it's still not working. I storing the array like so: . And then I'm using the foreach code from the link above to iterate over it. But literally nothing comes out. Should I edit my question and post the whole code? – shrewdbeans Mar 11 '12 at 11:29
  • Obmerk, I've edited the post above with my new code, thanks again. – shrewdbeans Mar 11 '12 at 14:10
0

It seems as though the problem lies with the database. I noticed that if I deleted certain posts, then it would start working fine. I'm yet to find out what's wrong with these posts though, if I delete them and then re-create them, they're usually fine.

My advice to anyone with this problem is try moving some of your posts to the trash and see if it starts working.

shrewdbeans
  • 11,971
  • 23
  • 69
  • 115