0

Here is my search function in functions.php

function search_filter($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( ($query->is_tag() ) || ( $query-> is_search() ) ) {
           $query->set( 'post_type', array( 'post', 'portfolio' ) );// this doesn't work (no portfolio shows up)
            //$query->set( 'post_type', 'post', 'portfolio' ); // this seems to work better I don't know why
        }
    }
}
add_action( 'pre_get_posts', 'search_filter' );

I got 2 php files: search.php and tag.php which are the same, actually.

get_header(); ?>
  <?php
   $exclude_ids = array( 101 );
   $loop = new WP_Query(array(
    'post__not_in' => $exclude_ids,
   )); ?>
    <?php if ( $loop ) : ?>
        <div id="container">
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <?php get_template_part( 'content', 'search' ); ?>
            <?php endwhile; ?>
        </div><div class="clearboth"></div>
        <?php input_pagination(); ?>
        <?php else : ?>
            <?php get_template_part( 'no-results', 'search' ); ?>
        <?php endif; ?>
<?php get_footer(); ?>

When I click on terms in tag cloud, the page retrieve posts and portfolio posts (more or less) but not the post of the tag clicked. For instance, I click on term "science" and the displayed posts don't have this term tagged.

Should I write a special new WP_Query into the tag.php page? I also need to exclude a page that was coming up that I don't want. I put this query into my tag.php file. That solve this special point, but doesn't show me up the appropriate post from tag I click on.

I know I missunderstand and do something wrong but any help will be grantly appreciated. Any direction to look for will give me hope. Any idea to how to debug even methods to how to proceed... Every solution I test fails and my deadline is... now. Thx.

1 Answer 1

0

For those who face the same issue, I post my solution that seems to work (I am keeping my fingers crossed that I still need to test to see if everything is running well). I found the solution thanks to this post

I removed the pre_get_posts that made my life miserable. I put the tag.php file with:

<?php get_header(); 
$term = get_queried_object();
$term_id = $term->term_id;  
$args = array(
  'post_type'      => array ('post', 'portfolio'),
  'posts_per_page' => -1,
  'post_status'    => 'publish',
  'tax_query'      => array(
     array(
       'taxonomy'    => 'post_tag',
       'field'       => 'term_id',
       'terms'       => [$term_id]
     )
   )
);
$query = new WP_Query($args); ?>
            <?php if ( $query->have_posts() ) : ?>
            <div id="container">
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>     
                    <?php get_template_part( 'content', 'search' ); ?>
                <?php endwhile; ?>
            </div><div class="clearboth"></div>
                <?php input_pagination(); ?>
            <?php else : ?>
                <?php get_template_part( 'no-results', 'search' ); ?>
            <?php endif; ?>
<?php get_footer(); ?>

And... it works (at least for tag). I don't know why it was so messy. But I should thank everybody who read my long question.

5
  • You'd likely run into a pagination conflict, if the main query had less number of posts or pages than your custom query. The proper way, which avoids double database request, is by using the pre_get_posts action to filter the main query args. Which means, your tag template will only need to use the basic/main loop to display the posts in the main query.
    – Sally CJ
    Feb 4 at 1:45
  • Yes! Thx for your comment. That's what I wanted to do. But I faced so many problems with the pre_get_posts that I used the oldest solution to be sure that it works. i assume there is some conflicts with the theme I have been using. Unfortunately, no time to find the way in order to. But you're right. The better solution would be to filter the main query args and then using simple template to display posts.
    – made leod
    Feb 4 at 10:18
  • Actually, what I also had in mind is, there's another callback which was overriding the post_type value, so, maybe all you needed to do is just use a greater number as the priority of your pre_get_posts callback, e.g. add_action( 'pre_get_posts', 'search_filter', 20 ); - note the 20.
    – Sally CJ
    Feb 5 at 15:46
  • Also, what does the input_pagination() do? If it is to display pagination links (to pages 2, 3, etc.), then why is that 'posts_per_page' => -1? Are you aware that it's equivalent to 'nopaging' => true which means pagination is disabled?
    – Sally CJ
    Feb 5 at 16:05
  • 1
    Good Point, I've changed it for 'posts_per_page' => 6, after testing it in order to have the input_pagination() working. Thx
    – made leod
    Feb 7 at 15:51

Your Answer

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

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