Support

Account

Home Forums Front-end Issues Show All Posts with True on True/False Field

Solving

Show All Posts with True on True/False Field

    • weyakin

    • November 3, 2016 at 5:43 am

    I am trying to display in my template linked titles of posts that have “True” checked off on my True/False custom field. So far, I have only successfully gotten one to show up. I have tried a few ways to display the whole loop of posts, but the best I’ve gotten is just the most recent post to display. Or nothing displays at all.

    What I’m aiming to display is a list like this:

    – Event Name Here
    – Another Great Event Name
    – This Event is also Special

    This is the code that displays just the most recent post:

    <?php
    
    /*
    *  View value data (for debugging)
    */
    
    // var_dump( get_field('signature_event') );
    
    /*
    *  Conditional Statement
    */
    
    if( get_field('signature_event') )
    {
        echo "<a href='".get_page_link()."'>".get_the_title()."</a>";
    }
    else
    {
        echo "";
    }
    
    /*
    *  Query posts for a true/false value.
    *  This method uses the meta_query param to match the string "1" to the database value "1|0"
    */
    
    $posts = get_posts(array(
    	'meta_query' => array(
    		'post_type' => 'events',
    		array(
    			'key' => 'signature_event',
    			'value' => '1',
    			'compare' => '=='
    		)
    	)
    ));
    
    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
    	}
    
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    }
    
    ?>

    This is the code for the loop that displays nothing:

    <?php
    
    	    $the_query = new WP_Query( 
    	    	array('meta_query' => array(
    				'post_type' => 'events',
    				array(
    					'key' => 'signature_event',
    					'value' => '1',
    					'compare' => '=='
    				)
    			))
    	    );
    
    		?>
    
    		<?php if ( $the_query->have_posts() ) : ?>
    
    		    <?php 
    		        // True/False field to show featured area image and featured area title fields
    		        if( get_field('signature_event') ) :  
    		        // If set to true, then show the following:
    		    ?>
    
    			<ul>
    			<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    				<li><a href="<?php page_link(); ?>"><?php the_title(); ?></a></li>
    			<?php endwhile; ?>
    			<?php wp_reset_postdata(); ?>
    			</ul>
    
    			<?php endif; ?>
    
    		<?php endif; ?>

    Thanks for any insight!

  • The query should look like this, I’ve separated the arguments from the function so that they are easier to see.

    
    $args = array(
      'post_type' => 'events',
      'posts_per_page' => -1,
      'meta_query' => array(
        array(
          'key' => 'signature_event',
          'value' => 'signature_event',
          'compare' => '==' // not really needed, this is the default
        )
      )
    );
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
      // This
      // if( get_field('signature_event') )
      // isn't needed since we did a query to only get ones
      // with a true value
     ?>
        <ul>
          <?php 
            while ($the_query->have_posts()) {
              $the_query->the_post();
              ?>
                <li><a href="<?php page_link(); ?>"><?php the_title(); ?></a></li>
              <?php 
            } // end while have posts
          ?>
        </ul>
      <?php
      wp_reset_postdata();
    } // end if have posts
    
    • weyakin

    • November 9, 2016 at 1:56 am

    Thanks John. Unfortunately nothing shows up when I use this method, just like the version I made. Any other ideas? Thanks!

  • That could be because I have the wrong value in the arguments “signature_event” instead of “1”. Basically what I was trying to highlight is that you had your query arguments nested improperly.

    
    $args = array(
      'post_type' => 'events',
      'posts_per_page' => -1,
      'meta_query' => array(
        array(
          'key' => 'signature_event',
          'value' => '1',
          'compare' => '==' // not really needed, this is the default
        )
      )
    );
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
      // This
      // if( get_field('signature_event') )
      // isn't needed since we did a query to only get ones
      // with a true value
     ?>
        <ul>
          <?php 
            while ($the_query->have_posts()) {
              $the_query->the_post();
              ?>
                <li><a href="<?php page_link(); ?>"><?php the_title(); ?></a></li>
              <?php 
            } // end while have posts
          ?>
        </ul>
      <?php
      wp_reset_postdata();
    } // end if have posts
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.