1

(Queries are definitively an infinite source of misunderstanding...)

I try to display two types of informations in a tax archive (taxonomy-artiste.php) :

  1. The content of a page from a custom post (bio)
  2. And after, a list of post from another custom post (works)

If I understood well, it could be interesting to use pre_get_post to change the main query. So, I set that in my function.php.

//Include all my CPT but the bio
function lm_exclude_bio( $query ) {
  if( is_tax('artiste') && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'cpt#1', 'cpt#2'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'lm_exclude_bio' );

Ok, it works.

But, when I add a new query in this archive, it doesn't display the post I excepted...

$args = array( 'post_type' => 'bio', 'posts_per_page' => 1 );
// My second query for CPT 'bio'
$bio_query = new WP_Query( $args );

// The Loop
if ( $bio_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $bio_query->the_post();
        // please, my custom post 'bio' !...
    }
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
?>

Is it impossible( stupid ?) to set two queries with opposite arguments ? Thanks for any help !

2 Answers 2

3

You've added the filter lm_exclude_bio to pre_get_posts. So when you need to run another query you can remove the filter to get normal query. You can remove the filter like below-

// Here we're removing the filter first. Then we are running the query.
remove_filter( 'pre_get_posts', 'lm_exclude_bio' );

$args = array( 'post_type' => 'bio', 'posts_per_page' => 1 );
// My second query for CPT 'bio'
$bio_query = new WP_Query( $args );

// The Loop
if ( $bio_query->have_posts() ) {
    while ( $bio_query->have_posts() ) {
        $bio_query->the_post();
        // please, my custom post 'bio' !...
    }
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}
?>

Also in while ( $the_query->have_posts() ) you got a error on $the_query. It would be $bio_query. I've fixed the error in my above code.

Hope it helps.

4
  • Thank you, the <code>remove_filter</code> works well ! But, my 'bio query' still behaves strangly... I mean : post per page is ok, but it doesn't diplay the called post 'bio... I try with a 'template_part', and it was worth : It display some hollow post, without any content, which takes me to a 404... I looked with attention at my custom type and taxonomies records, withou finding explaination. I will take a look in my database...
    – Cha
    Jan 11, 2017 at 13:34
  • (Sorry, it must seem very confused...)
    – Cha
    Jan 11, 2017 at 13:36
  • I just tested it again and it's totally working fine in my system. By the way if this helped you, can you please accept the answer ? @Cha
    – CodeMascot
    Jan 11, 2017 at 14:26
  • I think somehow you removed the accept check sign. It would be better if you check it. Thanks. @Cha
    – CodeMascot
    Jan 11, 2017 at 18:22
1

Ok, first of all I found a misleading in your code, check the while part I ammended:

// The Loop
if ( $bio_query->have_posts() ) {
    while ( $bio_query->have_posts() ) {
        $bio_query->the_post();
        // please, my custom post 'bio' !...
    }
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
?>

BUT I think you are messing with the queries because you're using two different ways to attack the WP database. In your first piece of code (I guess you put it into your functions.php file), your are interfering with the 'main query' object, but It seems you are never using it in your taxonomy-artiste.php file. I suggest to make something like this:

<?php
// Taxonomy-artiste.php


/*
 * The content of a page from a custom post (bio)
 */

$args = array( 'post_type' => 'bio', 'posts_per_page' => 1 );
$bio_query = new WP_Query( $args );

// The Loop
if ( $bio_query->have_posts() ) {
    while ( $bio_query->have_posts() ) {
        $bio_query->the_post();
        the_content() // This will print the content of your bio post, stored in the global $post variable
    }
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}

/*
 * And after, a list of post from another custom post 
 */

$args = array( 'post_type' => '#cpt1', 'posts_per_page' => 'put here the number of #cpt1 you want to load' );
$other_query = new WP_Query( $args );

// The Loop
if ( $other_query->have_posts() ) {
    while ( $other_query->have_posts() ) {
        $other_query->the_post();
        the_content() // This will print the content of your other posts, stored in the global $post variable
    }
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}

?>

I hope it will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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