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.

I'm writing a template to display my custom posts types in a list. But I only want to have the post show if it has a value for one of my custom fields. I'll explain: My custom post type is "Faculty Members" and the Custom Field is "awards" The page will display all Faculty members who have received awards and what there awards are. Right now it is just posting All Faculty members thought the IF function is there so that my "Awards:" list is only displayed when there is a value. how can I apply this if function to all posts?

<?php/*Template Name: Prizes & Awards*/?>
<?php get_header(); ?>

<div id="content" class="wrapper">
<?php query_posts( 'post_type=mtt_page'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class('page'); ?>>
        <article>
            <h1><?php the_title(); ?></h1>
            >
            <?php if ( has_post_thumbnail() ) { /* loades the post's featured thumbnail, requires Wordpress 3.0+ */ echo '<div class="featured-thumbnail">'; the_post_thumbnail(); echo '</div>'; } ?>

            <div class="post-content page-content">
                <?php the_content(); ?>
/*This is the custom field*/
<?php
$awards = types_render_field("awards", array("raw"=>"true","separator"=>"<li>", ‘show_name’ => ‘true’));
if( !empty( $awards ) ) {
echo "<b>Prizes and Awards:</b><li>".$awards."</li>";}
?>

            <?php wp_link_pages('before=<div class="pagination">&after=</div>'); ?>
            </div><!--.post-content .page-content -->
        </article>
    </div><!--#post-# .post-->



<?php endwhile; ?>

share|improve this question
1  
What is the types_render_field() function? Where it goes from? –  Max Yudin Mar 27 '13 at 19:50
1  
Under no circumstances should you use query_posts, use WP_Query instead –  Tom J Nowell Mar 27 '13 at 20:50
add comment

1 Answer 1

up vote 1 down vote accepted

If I understand you, you want to filter out faculty members who do not have any awards at the query stage.

Replace your code:

<?php query_posts( 'post_type=mtt_page'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

with this (untested):

<?php
$args = array(
    'post_type' => 'mtt_page',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'awards',
            'compare' => 'EXISTS'
        )
    )
);
$members = new WP_Query( $args );
?>
<?php if ( $members->have_posts() ) while ( $members->have_posts() ) : $members->the_post(); ?>

Note that I believe the EXISTS meta_query comparison only works in WP 3.5 onwards.

As a few people have pointed out - query_posts is best avoided.

share|improve this answer
1  
Rewrite with a proper WP_Query() loop, and I'll change the -1 to a +1. –  Chip Bennett Mar 27 '13 at 20:02
    
Types_render_field is a field created by the plugin "Types" and it adds a metabox. It was much easier to use the plugin because it's a repeatable field and those are just a pain, I don't care how many repeatable field tuturials there are I couldn't get it working. But the value could be anything. Anything but nothing. As long as it has something I want it to list that post. But most of them have nothing –  user1855130 Mar 27 '13 at 20:44
1  
I edited my answer accordingly. –  vancoder Mar 27 '13 at 21:27
    
Thanks! That worked great! one last question hopefully its not a big deal. I can't seem to set it to post all. Previously it was posting 10 posts out of about 50. Now it is posting the 3 out of the 10 that have a value for that(which is what I wanted but now I want it too look through all of my posts). I tried setting post_count to 9999 and no pagination but that didn't seem to work –  user1855130 Mar 27 '13 at 22:03
    
I've added posts_per_page to my answer. –  vancoder Mar 27 '13 at 22:07
show 2 more comments

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.