• I am having trouble getting a wp_query to work properly. I have three different Custom Post Types, all of which have categories attached to them.

    I have set up a Custom Field Type called ‘Category Number’ so that the user can create a page and loop the posts of any category below it, without the need for a developer to create a different template each time.

    The code I am currently using (Code snippet 1) is the closest I can get to working properly (after trying everything I can think of!) however it will only work on the first CPT listed in the functions file, and not the others!

    I have tried to get around this by specifying the post types within the loop (Code snippet 2). While this allows the loop to work for all the CPTs, it then pulls in all of them, ignoring the user specified category number.

    Thanks, Any help would be hugely appreciated!

    This code allows the category to be chose by the user, but only for the first CPT:

    <?php $categorynumber = get_post_meta($post->ID, 'Category Number', true); ?>
    
    <?php query_posts('cat=' . $categorynumber); ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
       /*Loop Content*/ 
    
    <?php endwhile; endif; ?>
    
    <?php wp_reset_query(); ?>

    This is the code that fixes it so that all CPTs work, but the category does not:

    <?php $categorynumber = get_post_meta($post->ID, 'Category Number', true); ?>
    
    <?php query_posts( 'post_type' => array( 'lathes', 'barfeeds', 'accessories' ),'cat=' . $categorynumber); ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
       /*Loop Content*/ 
    
    <?php endwhile; endif; ?>
    
    <?php wp_reset_query(); ?>

    Thanks again

Viewing 5 replies - 1 through 5 (of 5 total)
  • Robbie,

    On your second code block, shouldn’t the 2nd line be:

    <?php query_posts( 'post_type' => array( 'lathes', 'barfeeds', 'accessories' ),'cat' => $categorynumber); ?>

    Haven’t tried it, but should work.

    Ryan

    Thread Starter jengstromdesign

    (@jengstromdesign)

    Hi Ryan,

    Thanks but I already tried that. It brings up a sever error and doesnt work at all!

    Also tried using $args to state the loop parameters and get the same result, as in all 3 CPTs work, but the category does not.

    I think you are right, the issue should be with the sytax of that line, but nothing I have tried works, and I can’t find the solution anywhere!

    Jon

    Sorry Jon (got wrong name from tweet!)

    Have you tried using WP_Query?

    Ryan

    Thread Starter jengstromdesign

    (@jengstromdesign)

    No problem, I asked Rob to post it out on twitter for me!

    Just tried using new WP_Query as below, but I get the same result as my first block of code, in that it works but only for the first CPT.

    <?php $categorynumber = get_post_meta($post->ID, 'Category Number', true); ?>
    
    <?php $args=array(
      'cat' => $categorynumber,
      'post_type' => array( 'lathes', 'barfeeds', 'accessories' ),
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    
    $new = new WP_Query($args); ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
       /*Loop Content*/ 
    
    <?php endwhile; endif; ?>
    
    <?php wp_reset_postdata(); ?>

    i also have same problems!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Multiple CPT in a single Loop’ is closed to new replies.