Loop the latest post-type that has a meta box checked only

Posted on 2012-08-16
Last Modified: 2012-08-16
I need to loop only 1 of the latest post-type post in my sidebar, however, I only want to loop the latest one that has a meta box checked. I use the metabox option to give the post-type two different looks (videos or photos).
I want to loop the photo type only and the problem is that if a video version of that post-type is the newest one, then it blows out the older one and displays nothing. Is there a way to do this by effecting the query arguments or something?

Here is what I have so far:

<?php $args = array( 'post_type' => 'my_post_type', '', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php   if(get_post_meta($post->ID, 'my_post_type', true)) : ?>	
<?php the_title(); ?>
<?php endif; ?>
<?php endwhile; ?>

Open in new window

Question by:adrian7474
    1 Comment

    Accepted Solution

    I got it. Answered my own question. I added this to the arguments:

    $args = array( 'post_type' => 'my_post_type', 'meta_key' => 'my_meta_key', 'true', 'posts_per_page' => 1 );

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now