【问题标题】:Tax_query not working in wordpress. how to solve?Tax_query 在 wordpress 中不起作用。怎么解决?
【发布时间】:2019-12-02 09:59:23
【问题描述】:

我制作了一个页面模板,我可以在其中过滤一些帖子。如果你做一个正常的 WP_Query 一切都很好。但如果我使用 tax_query,它不会显示任何帖子。

为了注册帖子类型和分类,我使用插件“cptui”。

常规的 WP_Query 请求:

$query = new WP_Query(array(
    'post_type' => $post->post_name,
    'post_status' => 'publish',
    'posts_per_page' => 6,
    'paged' => $paged,
                                    
));

if(!$_POST) { //If not filtered ... then show all
    if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
        echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
        echo "<h5>" . $query->post->post_title . "</h5>";
        echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
        echo "</div></a>";
    endwhile; endif;
}

我阅读了其他帖子,他们说我可以使用 php echo $GLOBALS['query']-&gt;request; 来查看 mysql 查询是什么。

我的查询是:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
WHERE 1 = 1 
  AND wp_posts.post_type = 'vacature' 
  AND ((wp_posts.post_status = 'publish')) 
ORDER BY wp_posts.post_date DESC 
LIMIT 6, 6

现在是带有 tax_query 的请求。

tax_query WP_Query 请求:

$query = new WP_Query(array(
    'post_type' => $post->post_name,
    'post_status' => 'publish',
    'posts_per_page' => 6,
    'paged' => $paged,
    'tax_query' => array(
        array(
           'taxonomy' => 'provincie',
            'field'    => 'slug',
            'terms'    => 'noord-brabant',
            'include_children'  => 0
        ),
    ),
));

if(!$_POST) { //If not filtered ... then show all
    if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
        echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
        echo "<h5>" . $query->post->post_title . "</h5>";
        echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
        echo "</div></a>";
    endwhile; endif;
}

我现在得到的查询是:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1 = 1 
  AND (wp_term_relationships.term_taxonomy_id IN (3)) 
  AND wp_posts.post_type = 'vacature' 
  AND ((wp_posts.post_status = 'publish')) 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC 
LIMIT 6, 6  

我没有收到回复。

你知道有什么解决办法吗?

模板的完整代码:

<div id="page-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <?php the_content(); ?>
                            <?php   
                                global $post;  

                                $paged = ( get_query_var('paged')) ? get_query_var('paged') : 1;

                                $query = new WP_Query(array(
                                    'post_type' => $post->post_name,
                                    'post_status' => 'publish',
                                    'posts_per_page' => 6,
                                    'paged' => $paged,
                                    'tax_query' => array(
                                        array(
                                            'taxonomy' => 'provincie',
                                            'field' => 'slug',
                                            'terms' => array( 'noord-brabant')
                                        )
                                    )
                                ));

                                // echo $GLOBALS['query']->request;

                                if(!$_POST) { //If not filtered ... then show all
                                    if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
                                        echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
                                        echo "<h5>" . $query->post->post_title . "</h5>";
                                        echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
                                        echo "</div></a>";
                                    endwhile; endif;
                                }
                                else { //now its filtered
                                    $null = true;
                                    if($query->have_posts()): while ($query->have_posts()) : $query->the_post();

                                        $filterconditions = []; //hier komen alle filtercondities in
                                    
                                        $keys = array_keys($_POST);
                                        foreach($keys as $key) {
                                            if($_POST[$key] == "on") {
                                                $filter = explode("_",$key);
                                                $field = $filter[0];
                                                $filter = $filter[1];
                                                
                                                $taxonomy = get_field( $field );
                                                $term = get_term_by('term_id', $taxonomy, $field);
                                                array_push($filterconditions, '"' . $filter . '"' . '==' . '"' . $term->name . '"');
                                            }
                                        };

                                        $filterconditions = implode('&&', $filterconditions);
                                        // echo $filterconditions;
                                        if(eval("return $filterconditions;")) {
                                            $null = false;
                                            echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
                                            echo "<h5>" . $query->post->post_title . "</h5>";
                                            echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
                                            echo "</div></a>";
                                        }
                                    endwhile; endif;
                                    if($null) {
                                        echo "Sorry, er zijn geen vacatures gevonden met deze filters";
                                    }
                                }

                                $total_pages = $query->max_num_pages;
                                if ($total_pages > 1 && !$null) {
                                    $current_page = max(1, get_query_var('paged'));
                                    echo paginate_links(array(
                                        'base' => get_pagenum_link(1) . '%_%',
                                        'format' => '/page/%#%',
                                        'current' => $current_page,
                                        'total' => $total_pages,
                                        'prev_text'    => __('« prev'),
                                        'next_text'    => __('next »'),
                                    ));
                                } 

                                wp_reset_query();
                            ?>
                        </div>

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:
    $args=array(
    'post_type' => 'your_post_type',
    'post_status' => 'publish',
    'orderby' => 'title',
    'order' => 'DESC',
    'posts_per_page' => 6,
    'tax_query' => array(
    array(
    'taxonomy' => 'provincie',
    'field' => 'slug',
    'terms' => array( 'noord-brabant')
    )
    )
    );
    
    $result = new WP_Query( $args );
    
    // The Loop
    if ( $result->have_posts() )  {
    while ( $result->have_posts() ) {
    $result->the_post();
    
    // this is your loop
    
    }
    
    } else {
    // nothing
    }
    

    可以试试上面的代码

    【讨论】:

    • 感谢您的帮助,但它并没有为我解决问题。我在条款上尝试了 array() 并采取了你的循环。结果仍然没有帖子。
    • 你的 post_type 名称是什么? $post->post_name 你能从这个变量中得到什么?可以打印
    • '假期'。那是荷兰人的工作机会
    • 你能不能通过vacature static 和检查!
    • 你可以通过在模板中添加global $post;来获得$post->post_name
    【解决方案2】:

    您可以使用以下代码获取简单的自定义帖子类型列表:

    $args = array(  
            'post_type' => 'services',
            'post_status' => 'publish',
            'posts_per_page' => 6, 
            'orderby’ => 'title', 
            'order’ => 'ASC', 
        );
    
        $loop = new WP_Query( $args ); 
    
        while ( $loop->have_posts() ) : $loop->the_post(); 
            print the_title(); 
            the_excerpt(); 
        endwhile;
    
        wp_reset_postdata(); 
    

    放置您的自定义帖子类型名称以检索帖子并根据您的需要更改 while 循环

    【讨论】:

    • 我可以得到一个简单的帖子。那不是它的意义所在。如果我使用没有 tax_query 的 wpquery,它可以正常工作。但如果我使用 tax_query 它不会显示任何帖子。
    【解决方案3】:

    好的,您可以使用 get post 方法代替 WP Query:

    $posts_array = get_posts(
            array(
                'posts_per_page' => 6,
                'post_type' => $post->post_name, // Get POST TYPE FROM $GLOBAL POST
                'tax_query' => array(
                    array(
                        'taxonomy' => 'provincie',
                        'field' => 'slug',
                        'terms' => 'noord-brabant',
              )
            )
        )
    );
    

    【讨论】:

    • 我在这个请求中得到了 array(0) { }
    • 没有 tax_query 我可以找回我的帖子
    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2023-02-08
    • 1970-01-01
    • 2014-09-14
    • 2021-08-07
    相关资源
    最近更新 更多