Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

this is the correct solution

<?php $meta_cat_name = get_post_meta( $post->ID, 'related blog articles', true ); if( !empty( $meta_cat_name ) ): $my_query = new WP_Query( array( 'category_name' => $meta_cat_name, 'posts_per_page' => 5, ) ); if ( $my_query->have_posts() ): while ( $my_query->have_posts() ) : $my_query->the_post(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <?php endwhile; endif; endif; wp_reset_query(); ?>

----- original request and old code below -----

As I have yet to find a tutorial that does what I need I am using the More Fields plugin so that I can add partially filled custom fields/meta boxes on the edit posts page.

These have the key already set so that the user only needs to enter a value - in this case a category name which will then display 5 posts from that cat.

This works fine unless the post is published without a value being entered. This causes the published post to also list the 5 most recent posts. I assume its because it has been instructed that something is going there but only has half the info.

So how do I make the chunk of code below only appear if a value has been entered?

thanks

James

<?php $my_query = new WP_Query(array(
'category_name'  => get_post_meta($post->ID, 'related blog articles', true),
'posts_per_page' => 5,
)); if ($my_query->have_posts()):  ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>   
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?> 
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
            <?php
            $metacatname = get_post_meta( $post->ID, 'related blog articles', true );

            if(!(empty( $metacatname ))):

                $my_query = new WP_Query( array(
                    'category_name'  => $meta_cat_name,
                    'posts_per_page' => 5,
                ) );

                if ( $my_query->have_posts() ):

                    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>   
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <?php
                    endwhile;

                endif;

            endif;

            wp_reset_query();
            ?>
share|improve this answer
    
thanks again - no syntax errors this time but there is still something up as it pulls in the 5 latest posts from any category rather than the one specified –  James Olney Sep 23 '11 at 11:14
    
<?php $meta_cat_name = get_post_meta( $post->ID, 'related blog articles', true ); if( !empty( $meta_cat_name ) ): $my_query = new WP_Query( array( 'category_name' => $meta_cat_name, 'posts_per_page' => 5, ) ); if ( $my_query->have_posts() ): while ( $my_query->have_posts() ) : $my_query->the_post(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <?php endwhile; endif; endif; wp_reset_query(); ?> –  Mohit Bumb Sep 23 '11 at 11:29
    
Copy and paste that above code that's milo code but this time no syntax error maybe he's code work this time –  Mohit Bumb Sep 23 '11 at 11:29
    
excellent! it works perfectly :) looks like there was one to many brackets before - thank you very much Milo and Mohit –  James Olney Sep 23 '11 at 11:50
add comment

move your get_post_meta call outside the query and check if it's not empty first:

<?php
$meta_cat_name = get_post_meta( $post->ID, 'related blog articles', true );

if( !empty( $meta_cat_name ) ):

    $my_query = new WP_Query( array(
        'category_name'  => $meta_cat_name,
        'posts_per_page' => 5,
    ) );

    if ( $my_query->have_posts() ):

        while ( $my_query->have_posts() ) : $my_query->the_post(); ?>   
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
        <?php endwhile;

    endif;

endif;

wp_reset_query();
?> 
share|improve this answer
    
Many thanks for this but I am getting a syntax error. Are we missing a closing bracket or something similar? –  James Olney Sep 23 '11 at 10:37
    
@user8824 - oops, sorry about that, it was late! –  Milo Sep 23 '11 at 15:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.