WordPress.org

Forums

[resolved] Do custom fields work inside a WP_Query loop inside a page? (22 posts)

  1. Tyssen
    Member
    Posted 7 years ago #

    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?

  2. bcatdc
    Member
    Posted 7 years ago #

    Arg. I'm having EXACTLY the same problem.
    Anyone out there have an answer?

  3. totalbigbrother
    Member
    Posted 7 years ago #

    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.

  4. Mark / t31os

    Posted 7 years ago #

    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.

  5. Tyssen
    Member
    Posted 7 years ago #

    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.

  6. Mark / t31os

    Posted 7 years ago #

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

  7. MichaelH
    Member
    Posted 7 years ago #

    Maybe obvious, but also check your custom field key

    site-url is not the same as site_url

  8. Tyssen
    Member
    Posted 7 years ago #

    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. :?

  9. Mark / t31os

    Posted 7 years ago #

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

  10. MichaelH
    Member
    Posted 7 years ago #

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

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

  11. Mark / t31os

    Posted 7 years ago #

    $single = true

    Just true is sufficient, is it not? :)

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

  12. MichaelH
    Member
    Posted 7 years ago #

    You betcha ;) Good catch. See Function_Reference/get_post_meta

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

  13. Tyssen
    Member
    Posted 7 years ago #

    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.

  14. Mark / t31os

    Posted 7 years ago #

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

  15. Tyssen
    Member
    Posted 7 years ago #

    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?

  16. Tyssen
    Member
    Posted 7 years ago #

    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>
  17. MichaelH
    Member
    Posted 7 years ago #

    Try your code in the template itself. Does it work?

  18. Tyssen
    Member
    Posted 7 years ago #

    Yep works in the template if placed outside the normal loop.

  19. MichaelH
    Member
    Posted 7 years ago #

    So would you agree that the problem is likely with the plugin php exec?

  20. Tyssen
    Member
    Posted 7 years ago #

    Actually, it hadn't occurred to me, but now that you point it out, then yes, that's probably where the problem lies.

  21. MichaelH
    Member
    Posted 7 years ago #

    Possible next steps:
    1. Just leave the code in the template.

    2. Using a get_posts with a foreach / setup_posts data loop instead of a new query / while structure. For examples see:
    http://www.daobydesign.com/blog/2007/07/modifying-wordpress-front-page/

    3. Look at another php exec widget.

  22. Tyssen
    Member
    Posted 7 years ago #

    Using get_posts instead did the trick! Thanks very much for the pointer. :)

Topic Closed

This topic has been closed to new replies.

About this Topic