• Hi,

    I have a query that is set up to display a featured post, to be retrieved by way of a custom field associated with the particular post. From what I have I can’t see anything wrong and I’ve been looking at it all too long now to perhaps notice something I may have overlooked? The query looks like this:

    <?php
         $featured = new WP_Query();
         $featured -> query('showposts=1');
         while($featured -> have_posts()) : $featured -> the_post();
    
                            $wp_query->in_the_loop = true;
                            $featured_ID = $post->ID;
          ?>
    
          <?php if (get_post_meta($post->ID, 'preview', true)) { ?>
    
           <div class="image_block">
           <img src="<?php echo get_post_meta($post->ID, 'preview', true); ?>" width="230" height="160" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
            </div>
    
            <?php } ?>
            <div class="text_block">
            <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
            <small>posted <?php the_time('jS M Y'); ?> in <?php the_category(', '); ?> tagged <?php the_tags(''); ?></small>
    
            <?php the_content('Read On?'); ?>
            </div>
    
            <?php endwhile; ?>

    With a custom field ‘preview’ associated with only the one post I wish to feature, the value of which is a url for the image to be used with the content of the featured post, should this not work?

    If I put an else statement in to display the string ‘No posts match your criteria’ the query returns this string, rather than the post. If I do not put in the else statement the query returns the latest post, but still NOT the post with the custom field.

    Is there something I’m doing obviously wrong?

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jondoe_264

    (@jondoe_264)

    A little more info which may help?

    As mentioned above, without an else statement the query returns the single most recent post. If the most recent post happens to be the one post assigned the ‘preview’ custom field, with a url value, then the image does show up where it is supposed to for the featured post.

    So it seems the custom field and its value are present/associated with the post, so how is it that the query isn’t identifying this and pulling in the custom field post, rather than the one most recent post?

    I’m confused.

    Any help greatly appreciated.

    Thread Starter jondoe_264

    (@jondoe_264)

    Anybody?

    ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query not retreiving custom field post’ is closed to new replies.