Title: Do custom fields work inside a WP_Query loop inside a page?
Last modified: August 19, 2016

---

# Do custom fields work inside a WP_Query loop inside a page?

 *  Resolved [Tyssen](https://wordpress.org/support/users/tyssen/)
 * (@tyssen)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/)
 * I’ve got a page which uses this to insert a series of items into the body of 
   the content (using exec-php plugin):
 *     ```
       <ul>
       <?php $my_query = new WP_Query('cat=XX');
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
       	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       <?php endwhile; ?>
       </ul>
       ```
   
 * That works fine except that now I want to replace the_permalink with a URL entered
   into a custom field, and if I do this:
 *     ```
       <ul>
       <?php $my_query = new WP_Query('cat=XX');
         while ($my_query->have_posts()) : $my_query->the_post();
       	$link = get_post_meta($post->ID, 'site-url', true); ?>
       	<li><a href="http://<?php echo $link; ?>"><?php the_title(); ?></a></li>
       <?php endwhile; ?>
       </ul>
       ```
   
 * the link comes out blank.
 * So, is it not possible to use custom fields in this manner, ie, inside a custom
   loop inside a normal WP loop?

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/page/2/?output_format=md)

 *  [bcatdc](https://wordpress.org/support/users/bcatdc/)
 * (@bcatdc)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002329)
 * Arg. I’m having EXACTLY the same problem.
    Anyone out there have an answer?
 *  [totalbigbrother](https://wordpress.org/support/users/totalbigbrother/)
 * (@totalbigbrother)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002330)
 * This might work..
 *     ```
       <ul>
       <?php $my_query = new WP_Query('cat=XX');
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
       	<li><a href="<?php $link = get_post_meta($post->ID, "site-url", $single = true); if ( $site-url ) { echo $site-url; } else { the_permalink(); } ?>"><?php the_title(); ?></a></li>
       <?php endwhile; ?>
       </ul>
       ```
   
 * Is that helpful? – That’s how i would of done it.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002331)
 * Dump or print the value of $link.. this part..
    `$link = get_post_meta($post-
   >ID, 'site-url', true);`
 * Onto the screen, while testing this will help determine the problem.
 * So after that line, add…something like…
 *     ```
       if($link) {
       print '<pre>';
       print_r($link);
       print '</pre>';
       } else {
       // If link isn't filled print the $post ID to see if it's grabbing the correct one and print a message...
       print $post->ID.'<br />';
       print 'No value for custom field found..';
       }
       ```
   
 * It would be my guess that the issue relates to this area in any case…
    `$link
   = get_post_meta($post->ID, 'site-url', true);`
 * I can help further if need be… this should be fairly straight-forward.
 *  Thread Starter [Tyssen](https://wordpress.org/support/users/tyssen/)
 * (@tyssen)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002332)
 * I tried your suggestion t31os_ and I get _No value for custom field found.._ 
   printed to the screen but not the value of $link or $post->ID.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002333)
 * Well if link is not printed then that tells us it’s not finding a match for the
   custom field…
 * Secondly if no $post ID is showing then that may be why…
 * Update the code to this now..
 *     ```
       if($link) {
         print '<pre>';print_r($link);print '</pre>';
       } else {
         if($post) { print '<pre>';print_r($post);print'</pre>'; }
         else { print '$post is empty'; }
       }
       ```
   
 * If you get $post is empty… then that suggests the reason the code does not function
   is because $post does not contain the data it would in a normal loop…
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002334)
 * Maybe obvious, but also check your custom field key
 * site-url is not the same as site_url
 *  Thread Starter [Tyssen](https://wordpress.org/support/users/tyssen/)
 * (@tyssen)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002335)
 * It’s not the name of the custom field that’s wrong, it’s that $post doesn’t contain
   any data because testing with t31os_’s updated code, I get _$post is empty_ which
   I’m confused about because it’ll print out
 * `<?php the_permalink(); ?><?php the_title(); ?>`
 * OK. 😕
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002336)
 * I wanted to be sure so i tested this locally…
 * I placed the following in my theme index.php before the normal loop… Created 
   a custom field called testy and opened up my page…
 *     ```
       <?php
       $test = new WP_Query('showposts=3');
   
       if($test->have_posts()) :
       while($test->have_posts()) : $test->the_post();
   
       $testy = get_post_meta($post->ID,'testy',true);
   
       the_title();
       print '<br />';
       if($testy) print $testy.'<br />';
   
       endwhile;
       endif;
       wp_reset_query();
       ?>
       ```
   
 * **Result:** It works expected… i get the value of the custom field shown when
   it exists for the given post…
    ** Note: Per code above, this line `$testy = get_post_meta(
   $post->ID,'testy',true);` must be inside the while loop of the custom query…
 * **Further Note:** $post does contain data, but i think this depends where and
   when in the code (me/you/we) dump/print it…
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002337)
 * Try `$my_query->post->ID` for retrieving the custom fields
 * `get_post_meta($my_query->post->ID, "site-url", $single = true);`
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002338)
 * `$single = true`
 * Just `true` is sufficient, is it not? 🙂
 * Confirmed though `$test->post->ID` also works…
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002339)
 * You betcha 😉 Good catch. See [Function_Reference/get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)
 * `get_post_meta($my_query->post->ID, "site-url", true);`
 *  Thread Starter [Tyssen](https://wordpress.org/support/users/tyssen/)
 * (@tyssen)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002340)
 * Michael, your suggestion doesn’t produce anything either so I’m assuming it’s
   a problem with calling a custom query from within a normal query.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002341)
 * Perhaps if you could post the full code for the file you’re coding?
    [http://wordpress.pastebin.com/](http://wordpress.pastebin.com/)
 *  Thread Starter [Tyssen](https://wordpress.org/support/users/tyssen/)
 * (@tyssen)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002342)
 * Would that be just the PHP I’m pasting into the page or the full code for the
   template that controls the page’s output?
 *  Thread Starter [Tyssen](https://wordpress.org/support/users/tyssen/)
 * (@tyssen)
 * [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/#post-1002343)
 * Well actually, in either case, I don’t really need to use pastebin, because it’s
   fairly simple.
 * **The page template:**
 *     ```
       <?php
       /*
       Template Name: Pages
       */
       ?>
       <?php get_header(); ?>
       <div id="content">
       	<h1><?php the_title(); ?></h1>
       	<?php if (have_posts()) : while (have_posts()) : the_post();
       	the_content();
       	endwhile; endif; ?>
       </div>
       <?php get_footer(); ?>
       ```
   
 * **And the code being called from within the page:**
 *     ```
       <p>Content:</p>
       <ul>
       <?php $my_query = new WP_Query('cat=XX');
         while ($my_query->have_posts()) : $my_query->the_post();
       $link = get_post_meta($my_query->ID, site-url, true); ?>
       	<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
       <?php endwhile; ?>
       </ul>
       <p>More content.</p>
       ```
   

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/page/2/?output_format=md)

The topic ‘Do custom fields work inside a WP_Query loop inside a page?’ is closed
to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 21 replies
 * 5 participants
 * Last reply from: [Tyssen](https://wordpress.org/support/users/tyssen/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/do-custom-fields-work-inside-a-wp_query-loop-inside-a-page/page/2/#post-1002350)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
