WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Do custom fields work inside a WP_Query loop inside a page?

[Resolved] 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)
  • Arg. I’m having EXACTLY the same problem.
    Anyone out there have an answer?

    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.

    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.

    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.

    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…

    Maybe obvious, but also check your custom field key

    site-url is not the same as site_url

    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. 😕

    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…

    Try $my_query->post->ID for retrieving the custom fields

    get_post_meta($my_query->post->ID, "site-url", $single = true);

    $single = true

    Just true is sufficient, is it not? 🙂

    Confirmed though $test->post->ID also works…

    You betcha 😉 Good catch. See Function_Reference/get_post_meta

    get_post_meta($my_query->post->ID, "site-url", true);

    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.

    Perhaps if you could post the full code for the file you’re coding?
    http://wordpress.pastebin.com/

    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?

    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)
  • The topic ‘[Resolved] Do custom fields work inside a WP_Query loop inside a page?’ is closed to new replies.
Skip to toolbar