【问题标题】:Why I can't make use of WP_QUERY for my custom post type?为什么我不能将 WP_QUERY 用于我的自定义帖子类型?
【发布时间】:2019-05-11 12:50:35
【问题描述】:

我已经创建了一个相关的 post 函数并将其添加到 wordpress functions.php 中。

function related_posts($args = array()) {
    global $post;

    // default args
    $args = wp_parse_args($args, array(
        'post_id' => !empty($post) ? $post->ID : '',
        'taxonomy' => 'category',
        'limit' => 4,
        'post_type' => !empty($post) ? $post->post_type : 'post',
        'orderby' => 'date',
        'order' => 'DESC'
    ));

    // check taxonomy
    if (!taxonomy_exists($args['taxonomy'])) {
        return;
    }

    // post taxonomies
    $taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids'));
    if (empty($taxonomies)) {
        return;
    }

    // query
    //  $related_posts = get_posts(array(
    $related_posts = new WP_Query(array(
        'post__not_in' => (array) $args['post_id'],
        'post_type' => $args['post_type'],
                'tax_query' => array(
                array(
                   'taxonomy' => $args['taxonomy'],
                   'field' => 'term_id',
                   'terms' => $taxonomies
                ),
            ),
            'posts_per_page' => $args['limit'],
            'orderby' => $args['orderby'],
            'order' => $args['order']
        ));

        if  $related_posts ) {
            echo 'ok';
        } else {
            echo 'not ok';
        }
    ?>
    <?php if (!empty($related_posts)) { ?>
        <h3 class="widget-title"><?php _e('<h5 class="title is-6">You Might Also Like</h5>', 'http://localhost/wordpress_john/wordpress1/'); ?></h3>

        <div class="columns  ">
        <?php
           include( locate_template('related-posts-template.php', false, false) );
        ?>
        </div>
        <?php
        }
        ?>
        <?php
             wp_reset_postdata();
        }

        // related posts
        add_action( 'comment_form_before', 'related_posts', 10, 0 ) ;

我创建了一个自定义帖子 ( post_type => 'custom' ) 及其模板等,它工作正常。但是,当查看者查看从 single-custom.php 提供的自定义帖子时,此代码未显示相关帖子 最初这段代码使用 get_posts,我将其转换为 WP_QUERY,因为代码从 get_posts 返回为空,为什么? Single.php 的帖子显示了它们应该显示的相关帖子,但不是 single-custom.php。然后我转换为 WP_QUERY 因为 get_posts 有点受限然后 WP_QUERY 并且仍然,自定义帖子(single-custom.php )没有显示相关帖子,但变量 $related_posts 正在此处填充。救命!

【问题讨论】:

    标签: wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    试试这个工作代码:

     <?php $customTaxonomyTerms = wp_get_object_terms( $post->ID, 'category', array('fields' => 'ids') );
    
            $args = array(
                'post_type' => 'events',
                'post_status' => 'publish',
                'posts_per_page' => 5,
                'orderby' => 'rand',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'field' => 'id',
                        'terms' => $customTaxonomyTerms
                    )
                ),
                'post__not_in' => array ($post->ID),
            );
    
            //the query
            $relatedPosts = new WP_Query( $args );
    
            //loop through query
            if($relatedPosts->have_posts()){
                echo '<ul>';
                while($relatedPosts->have_posts()){ 
                   $relatedPosts->the_post(); ?>
                   <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                   <?php } echo '</ul>';
                 }else{
                //no posts found
                }
              wp_reset_postdata();
            ?>
    

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多