I need multiple loops on a page and have created them as such

global $post;
$args = array( 'numberposts' => 3, 'category' => 10 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?> "><?php the_title();?></a></li>
<?php endforeach; ?>

I have a custom post type (foo_post_type) which should be used as a conditional while the loop runs. For example, for each post in this loop:

if ('foo_post_type' == 'xyz' || 'foo_post_type' == '0' && 'category' => 10) {
 ...then display in the loop.....

But I am able to grab the custom post type for each post in the loop before the loop runs. And if I grab the custom post type during the loop it returns the same (usually incorrect) value. Where can I stick this foo_post_type in the loop shown at the top so it displays and evaluates each post bringing back the latest 3 which adhere to each condition?

(Unfortunately, I do not have a public link to the site.)

  • 1
    To clarify, are you trying to only display posts of a certain type within the loop? If so, you can add the 'post_type' => 'foo_post_type' argument to your get_posts call and you'll only have posts with that type. – Andy Adams Jan 23 '13 at 17:43
  • Thanks Andy. foo_post_type is a drop down with it's own selections. Can I do an arg like: 'post_type' => 'foo_post_type' => 'foo1' – Potato Jan 23 '13 at 18:48
  • that shouldn't matter. Presumably foo_post_type is available inside your custom loop, so it should also be available before you instantiate it, right? – Chip Bennett Jan 23 '13 at 18:51
  • Chris - yes, that's true. A better way to say it is that I want to push this: 'foo_post_type' == 'xyz' || 'foo_post_type' == '0' && 'category' => 10 into my $args statement. I've tried various ways but cannot get it to obey my request! – Potato Jan 23 '13 at 18:56
  • So is 'foo_post_type' a custom taxonomy attached to your custom post type? It's hard to tell from your heavily abstracted code. – heathenJesus Jan 23 '13 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.