Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 3 differents custom post type : "Projet", "Notice" and "Disque". I want them to appear in a specific order when they are listed in the category or the tag template page. Like that :

1 - Notice

2 - Projet

3 - Disque

I've tried to catch te query string and use 3 different loops with WP_Query with no results.

<?php $my_notices_query = new WP_Query($query_string.'&post_type=notices&posts_per_page=1'); // only notices… ?> 
<?php while ($my_notices_query->have_posts()) : $my_notices_query->the_post(); ?>

the query allways show all the cpt with no restriction. Thks for your help. BR

share|improve this question
    
I must say that I already add this code to make appear the cpt in my tag and category template page. –  johnfrancisco Aug 8 '12 at 10:56
add comment

2 Answers

someone there help me and solved the problem. It works. Cool. Thks guys.

Details : First - I remove this code from function.php

function namespace_add_custom_types( $query ) {
if( ( is_tag() || is_category() ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
 'post', 'projets', 'disques', 'notices', 'main_menu', 'top_menu'
    ));
  return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

(I used this to get my custom post type in category and tag template pages. It also prevent nav menu to disappear. Here I don't need it anymore, I will use multiple loops.)

Second - Use this code in category.php and tag.php :

<?php 

    $current_cat = get_query_var('cat');
    // for tags use 'tag'
    // $current_tag = get_query_var('tag');

    // --- First Loop --- Notices
    $args = array(
      'cat' => $current_cat,
      //'tag' => $current_tag,
      'post_type' => 'notices',

    );

    $the_first_query = new WP_Query( $args );
    // The Loop
    while ( $the_first_query->have_posts() ) : $the_first_query->the_post();?>

        <?php the_title(); ?>
        <?php the_post_thumbnail(); ?>
        <?php the_excerpt(); ?>     

    <?php endwhile;
    // Reset Post Data
    wp_reset_postdata();
    // END --- First Loop --- 

    // --- Second Loop --- Projets
    $args = array(
      'cat' => $current_cat,
      // 'tag' => $current_tag,
      'post_type' => 'projets',

    );

    $the_second_query = new WP_Query( $args );
    // The Loop
    while ( $the_second_query->have_posts() ) : $the_second_query->the_post();?>

        <?php the_title(); ?>
        <?php the_post_thumbnail(); ?>
        <?php the_excerpt(); ?>

    <?php endwhile;
    // Reset Post Data
    wp_reset_postdata();
    // END --- Second Loop ---

    // --- Third Loop --- Disques
    $args = array(
      'cat' => $current_cat,
      // 'tag' => $current_tag,
      'post_type' => 'disques',

    );

    $the_third_query = new WP_Query( $args );
    // The Loop
    while ( $the_third_query->have_posts() ) : $the_third_query->the_post();?>

        <?php the_title(); ?>
        <?php the_post_thumbnail(); ?>
        <?php the_excerpt(); ?>

    <?php endwhile;
    // Reset Post Data
    wp_reset_postdata();
    // END --- Third Loop --- 
?>

The bad thing is that breaks pagination… but in my case, it's not important. Thks to keesiemeijer from wordpress.org Forums for original answer.

share|improve this answer
    
Welcome to WordPress Answers! An answer should be more than just a link to an external site. Please add a solution. –  toscho Aug 8 '12 at 22:18
    
ok I will edit my answer… –  johnfrancisco Aug 9 '12 at 2:36
add comment
$args = array( 'post_type' => 'notices', 'posts_per_page' => 1 );
$loop = get_posts( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <!--- YOUR STUFF ------>

<?php endwhile; ?>
<?php wp_reset_query(); ?>

Not sure if you're calling 'Notice' or 'Notices' either.... but either way you can repeat the process for the other 2 in your template file.

share|improve this answer
    
Please read this in detail. It will allow you to get around one big mistake. :) –  kaiser Aug 7 '12 at 16:45
    
I'm not sure. I guess I understand that this is a second query aside from the main query. So, instead, I should define the 'arguments' prior to <?php get_header(); ?>? How would one do that 3 times like he is asking for in 1 template page then? –  RCNeil Aug 7 '12 at 17:39
    
Maybe you want to take a look at this diagram too. It's easier to understand :) –  kaiser Aug 7 '12 at 17:53
    
Mmmmmmm. So, use get_posts()? I'm not the most familiar with this, and while I have certainly been humbled by the votes and your constructive advice, providing the correct answer would probably be the most beneficial to me... and the OP. :) –  RCNeil Aug 7 '12 at 18:04
    
Absolutely. It's a community thing and everything got an "edit" button :) And yes, it's get_posts(). Every other solution would be better done with filters. –  kaiser Aug 7 '12 at 18:08
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.