Multiple CPT in a single Loop
-
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
- The topic ‘Multiple CPT in a single Loop’ is closed to new replies.