0

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(); ?> 

2 Answers 2

1
            <?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();
            ?>
4
  • 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 Sep 23, 2011 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, 2011 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, 2011 at 11:29
  • excellent! it works perfectly :) looks like there was one to many brackets before - thank you very much Milo and Mohit Sep 23, 2011 at 11:50
0

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();
?> 
2
  • Many thanks for this but I am getting a syntax error. Are we missing a closing bracket or something similar? Sep 23, 2011 at 10:37
  • @user8824 - oops, sorry about that, it was late!
    – Milo
    Sep 23, 2011 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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