查询特定用户ID发布的所有帖子

时间:2017-11-17 作者:DavSev

我想查询按用户id发布的所有帖子。

虽然这是基本循环,但我的代码有问题。

用户名为8的用户发布了两篇帖子。由于某种原因,结果我只收到一个帖子。原因是什么?

这是我的问题。

<?php 
    $args1 = array(
        \'author\' => 8,
        \'post_type\' => \'property\',
    );

    $custom_query = new WP_Query( $args1 );


    if ($custom_query->have_posts()) :
        while($custom_query->have_posts()) : $custom_query->the_post();
    ?>
        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </div>
    <?php
        endwhile;
    endif;
    wp_reset_postdata();
?>
这是的var转储$custom_query var\\u转储:

    object(WP_Query)#5055 (49) {
  ["query"]=>
  array(2) {
    ["author"]=>
    int(8)
    ["post_type"]=>
    string(8) "property"
  }
  ["query_vars"]=>
  array(64) {
    ["author"]=>
    string(1) "8"
    ["post_type"]=>
    string(8) "property"
    ["error"]=>
    string(0) ""
    ["m"]=>
    string(0) ""
    ["p"]=>
    int(0)
    ["post_parent"]=>
    string(0) ""
    ["subpost"]=>
    string(0) ""
    ["subpost_id"]=>
    string(0) ""
    ["attachment"]=>
    string(0) ""
    ["attachment_id"]=>
    int(0)
    ["name"]=>
    string(0) ""
    ["static"]=>
    string(0) ""
    ["pagename"]=>
    string(0) ""
    ["page_id"]=>
    int(0)
    ["second"]=>
    string(0) ""
    ["minute"]=>
    string(0) ""
    ["hour"]=>
    string(0) ""
    ["day"]=>
    int(0)
    ["monthnum"]=>
    int(0)
    ["year"]=>
    int(0)
    ["w"]=>
    int(0)
    ["category_name"]=>
    string(0) ""
    ["tag"]=>
    string(0) ""
    ["cat"]=>
    string(0) ""
    ["tag_id"]=>
    string(0) ""
    ["author_name"]=>
    string(0) ""
    ["feed"]=>
    string(0) ""
    ["tb"]=>
    string(0) ""
    ["paged"]=>
    int(0)
    ["meta_key"]=>
    string(0) ""
    ["meta_value"]=>
    string(0) ""
    ["preview"]=>
    string(0) ""
    ["s"]=>
    string(0) ""
    ["sentence"]=>
    string(0) ""
    ["title"]=>
    string(0) ""
    ["fields"]=>
    string(0) ""
    ["menu_order"]=>
    string(0) ""
    ["embed"]=>
    string(0) ""
    ["category__in"]=>
    array(0) {
    }
    ["category__not_in"]=>
    array(0) {
    }
    ["category__and"]=>
    array(0) {
    }
    ["post__in"]=>
    array(0) {
    }
    ["post__not_in"]=>
    array(0) {
    }
    ["post_name__in"]=>
    array(0) {
    }
    ["tag__in"]=>
    array(0) {
    }
    ["tag__not_in"]=>
    array(0) {
    }
    ["tag__and"]=>
    array(0) {
    }
    ["tag_slug__in"]=>
    array(0) {
    }
    ["tag_slug__and"]=>
    array(0) {
    }
    ["post_parent__in"]=>
    array(0) {
    }
    ["post_parent__not_in"]=>
    array(0) {
    }
    ["author__in"]=>
    array(1) {
      [0]=>
      int(8)
    }
    ["author__not_in"]=>
    array(0) {
    }
    ["ignore_sticky_posts"]=>
    bool(false)
    ["suppress_filters"]=>
    bool(false)
    ["cache_results"]=>
    bool(true)
    ["update_post_term_cache"]=>
    bool(true)
    ["lazy_load_term_meta"]=>
    bool(true)
    ["update_post_meta_cache"]=>
    bool(true)
    ["posts_per_page"]=>
    int(10)
    ["nopaging"]=>
    bool(false)
    ["comments_per_page"]=>
    string(2) "50"
    ["no_found_rows"]=>
    bool(false)
    ["order"]=>
    string(4) "DESC"
  }
  ["tax_query"]=>
  object(WP_Tax_Query)#5047 (6) {
    ["queries"]=>
    array(0) {
    }
    ["relation"]=>
    string(3) "AND"
    ["table_aliases":protected]=>
    array(0) {
    }
    ["queried_terms"]=>
    array(0) {
    }
    ["primary_table"]=>
    string(8) "wp_posts"
    ["primary_id_column"]=>
    string(2) "ID"
  }
  ["meta_query"]=>
  object(WP_Meta_Query)#5048 (9) {
    ["queries"]=>
    array(0) {
    }
    ["relation"]=>
    NULL
    ["meta_table"]=>
    NULL
    ["meta_id_column"]=>
    NULL
    ["primary_table"]=>
    NULL
    ["primary_id_column"]=>
    NULL
    ["table_aliases":protected]=>
    array(0) {
    }
    ["clauses":protected]=>
    array(0) {
    }
    ["has_or_relation":protected]=>
    bool(false)
  }
  ["date_query"]=>
  bool(false)
  ["request"]=>
  string(296) "SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_author IN (8)  AND wp_posts.post_type = \'property\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\' OR wp_posts.post_status = \'private\')  ORDER BY wp_posts.post_date DESC LIMIT 0, 10"
  ["posts"]=>
  array(1) {
    [0]=>
    object(WP_Post)#5046 (24) {
      ["ID"]=>
      int(363)
      ["post_author"]=>
      string(1) "8"
      ["post_date"]=>
      string(19) "2017-10-16 10:11:28"
      ["post_date_gmt"]=>
      string(19) "2017-10-16 07:11:28"
      ["post_content"]=>
      string(0) ""
      ["post_title"]=>
      string(44) "תל אביב 4 חדרים דו משפחתי"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(6) "closed"
      ["ping_status"]=>
      string(6) "closed"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(120) "%d7%aa%d7%9c-%d7%90%d7%91%d7%99%d7%91-4-%d7%97%d7%93%d7%a8%d7%99%d7%9d-%d7%93%d7%95-%d7%9e%d7%a9%d7%a4%d7%97%d7%aa%d7%99"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2017-11-07 07:58:37"
      ["post_modified_gmt"]=>
      string(19) "2017-11-07 05:58:37"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(57) "http://davdev.co.il/broker/?post_type=property&p=363"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(8) "property"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
  }
  ["post_count"]=>
  int(1)
  ["current_post"]=>
  int(-1)
  ["in_the_loop"]=>
  bool(false)
  ["post"]=>
  object(WP_Post)#5046 (24) {
    ["ID"]=>
    int(363)
    ["post_author"]=>
    string(1) "8"
    ["post_date"]=>
    string(19) "2017-10-16 10:11:28"
    ["post_date_gmt"]=>
    string(19) "2017-10-16 07:11:28"
    ["post_content"]=>
    string(0) ""
    ["post_title"]=>
    string(44) "תל אביב 4 חדרים דו משפחתי"
    ["post_excerpt"]=>
    string(0) ""
    ["post_status"]=>
    string(7) "publish"
    ["comment_status"]=>
    string(6) "closed"
    ["ping_status"]=>
    string(6) "closed"
    ["post_password"]=>
    string(0) ""
    ["post_name"]=>
    string(120) "%d7%aa%d7%9c-%d7%90%d7%91%d7%99%d7%91-4-%d7%97%d7%93%d7%a8%d7%99%d7%9d-%d7%93%d7%95-%d7%9e%d7%a9%d7%a4%d7%97%d7%aa%d7%99"
    ["to_ping"]=>
    string(0) ""
    ["pinged"]=>
    string(0) ""
    ["post_modified"]=>
    string(19) "2017-11-07 07:58:37"
    ["post_modified_gmt"]=>
    string(19) "2017-11-07 05:58:37"
    ["post_content_filtered"]=>
    string(0) ""
    ["post_parent"]=>
    int(0)
    ["guid"]=>
    string(57) "http://davdev.co.il/broker/?post_type=property&p=363"
    ["menu_order"]=>
    int(0)
    ["post_type"]=>
    string(8) "property"
    ["post_mime_type"]=>
    string(0) ""
    ["comment_count"]=>
    string(1) "0"
    ["filter"]=>
    string(3) "raw"
  }
  ["comment_count"]=>
  int(0)
  ["current_comment"]=>
  int(-1)
  ["found_posts"]=>
  string(1) "1"
  ["max_num_pages"]=>
  float(1)
  ["max_num_comment_pages"]=>
  int(0)
  ["is_single"]=>
  bool(false)
  ["is_preview"]=>
  bool(false)
  ["is_page"]=>
  bool(false)
  ["is_archive"]=>
  bool(true)
  ["is_date"]=>
  bool(false)
  ["is_year"]=>
  bool(false)
  ["is_month"]=>
  bool(false)
  ["is_day"]=>
  bool(false)
  ["is_time"]=>
  bool(false)
  ["is_author"]=>
  bool(true)
  ["is_category"]=>
  bool(false)
  ["is_tag"]=>
  bool(false)
  ["is_tax"]=>
  bool(false)
  ["is_search"]=>
  bool(false)
  ["is_feed"]=>
  bool(false)
  ["is_comment_feed"]=>
  bool(false)
  ["is_trackback"]=>
  bool(false)
  ["is_home"]=>
  bool(false)
  ["is_404"]=>
  bool(false)
  ["is_embed"]=>
  bool(false)
  ["is_paged"]=>
  bool(false)
  ["is_admin"]=>
  bool(false)
  ["is_attachment"]=>
  bool(false)
  ["is_singular"]=>
  bool(false)
  ["is_robots"]=>
  bool(false)
  ["is_posts_page"]=>
  bool(false)
  ["is_post_type_archive"]=>
  bool(true)
  ["query_vars_hash":"WP_Query":private]=>
  string(32) "8e707fefcd01a2398150df07917bd06c"
  ["query_vars_changed":"WP_Query":private]=>
  bool(false)
  ["thumbnails_cached"]=>
  bool(false)
  ["stopwords":"WP_Query":private]=>
  NULL
  ["compat_fields":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(15) "query_vars_hash"
    [1]=>
    string(18) "query_vars_changed"
  }
  ["compat_methods":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(16) "init_query_flags"
    [1]=>
    string(15) "parse_tax_query"
  }
}
缺少职位var_dump:

object(WP_Post)#5095 (24) { ["ID"]=> int(480) ["post_author"]=> string(1) "7" ["post_date"]=> string(19) "2017-11-17 11:35:30" ["post_date_gmt"]=> string(19) "2017-11-17 09:35:30" ["post_content"]=> string(0) "" ["post_title"]=> string(41) "בית בגדרה 3 חדרים קומה 1" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(109) "%d7%91%d7%99%d7%aa-%d7%91%d7%92%d7%93%d7%a8%d7%94-3-%d7%97%d7%93%d7%a8%d7%99%d7%9d-%d7%a7%d7%95%d7%9e%d7%94-1" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2017-11-17 11:36:05" ["post_modified_gmt"]=> string(19) "2017-11-17 09:36:05" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(54) "//127.0.0.1:3000/broker/?post_type=property&p=480" ["menu_order"]=> int(0) ["post_type"]=> string(8) "property" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

1 个回复
SO网友:Drupalizeme

您正在查询ID为8的帖子作者,并且要求将帖子类型与属性post type相匹配,并且post\\u状态为publish、acf disabled、private。

检查你的帖子,没有返回上述内容,并会找到原因。

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND 

wp_posts.post_author IN (8)  AND
wp_posts.post_type = \'property\' AND
(wp_posts.post_status = \'publish\' OR
 wp_posts.post_status = \'acf-disabled\' OR
 wp_posts.post_status = \'private\'
)  
ORDER BY wp_posts.post_date DESC LIMIT 0, 10"

结束

相关推荐

分页的Author.php不使用Author.php模板

我创造了一个作家。php显示作者的帖子,并分页。单击“下一步”按钮时,我会进入第二页,但使用的模板不同。有什么原因吗?My query in author.php$curauth = (get_query_var(\'author_name\')) ? get_user_by(\'slug\', get_query_var(\'author_name\')) : get_userdata(get_query_var(\'author\')); $paged = ( get_query_var( \