admin管理员组

文章数量:1125621

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.

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.

Share Improve this question edited Feb 3, 2024 at 15:35 made leod asked Feb 3, 2024 at 15:23 made leodmade leod 856 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: