【问题标题】:Wordpress - Query comments by post metaWordpress - 通过帖子元查询评论
【发布时间】:2017-07-25 21:52:14
【问题描述】:

我正在使用下面的代码(简化)来显示最后 10 个 cmets 的列表:

<?php 

$args = array(
    'post_type'      => 'tarefa',
    'number'         => '10',
    'order'          => 'DESC',
    'orderby'        => 'comment_date',
    //'meta_key'        => 'field_name',
    //'meta_value'      => 'field_value',
);

$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

foreach ( $comments as $comment ) {
    echo '<p>';
    echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
    echo $comment->comment_content; // comment content
    echo '</p>';
};

?>

问题:

嗯,meta_keymeta_value 似乎与 comment_meta 相关联......但就我而言,我必须根据post_meta 键和值。

有什么建议吗?

【问题讨论】:

    标签: wordpress comments post-meta


    【解决方案1】:

    你可以试试这个代码。 您需要为帖子添加查询以获取带有元键的帖子 ide 数组。 然后将该数组用于 cmets 查询参数。

    //QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
    $post_args = array(
        'post_type'  => 'post',
        'meta_key'     => 'meta key',//Meta key of post
        'meta_value'   => 'meta value',//String or Numeric value
        'meta_compare' => '=',
    );
    $post_query = new WP_Query( $post_args );
    $posts_array= array();
    if ( $post_query->have_posts() ) {
        while ( $post_query->have_posts() ) {
            $post_query->the_post();
    
            $posts_array[] = get_the_ID(); //Array of post ids
    
        }
        wp_reset_postdata();
    }
    
    
    
    //YOUR COMMENT ARGS SHOULD BE THIS
    $args = array(
        'post_type'      => 'tarefa',
        'number'         => '10',
        'order'          => 'DESC',
        'orderby'        => 'comment_date',
        'post__in'        => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
    );
    

    试试这个,然后告诉我结果。

    【讨论】:

      【解决方案2】:

      我在 Stackoverflow 的第一个问题非常有效。

      非常感谢您,苏维克!

      下面是最终结果(简化):

      $post_args = array(
        'post_type'              => 'tarefa',
        'posts_per_page'         => -1,
        'meta_key'               => 'field_name',
        'meta_value'             => 'field_value',
      );
      $post_query = new WP_Query( $post_args );
      $posts_array= array();
      if ( $post_query->have_posts() ) {
          while ( $post_query->have_posts() ) {
              $post_query->the_post();
              $posts_array[] = get_the_ID(); //Array of post ids
          }
          wp_reset_postdata();
      }
      
      //YOUR COMMENT ARGS SHOULD BE THIS
      $args = array(
          'number'         => '30',
          'order'          => 'DESC',
          'orderby'        => 'comment_date',
          'post__in'        => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
      );
      
      $comments_query = new WP_Comment_Query;
      $comments = $comments_query->query( $args );
      
      foreach ( $comments as $comment ) {
          echo '<p>';
          echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
          echo $comment->comment_content; // comment content
          echo '</p>';
      };
      

      【讨论】:

        猜你喜欢
        • 2017-07-29
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 1970-01-01
        • 2021-03-14
        • 2011-07-24
        相关资源
        最近更新 更多