0

I want to query all posts published by user id.

Something is wrong with my code though this is the basic loop.

The user with the userid = 8 posted two posts. For some reason, in the result, I receive only one post. What can be the reason for it?

this is my query.

<?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();
?>

This is the var dump of $custom_query var_dump:

    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"
  }
}

Missing post 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" }

11
  • 1
    Humor me and add 'posts_per_page' => -1, to the $args1 array please...
    – deflime
    Nov 17, 2017 at 9:47
  • 1
    Wordpress by default search for the first 10 posts starting from the 0: LIMIT 0, 10 Nov 17, 2017 at 9:52
  • 1
    Can you post a var dump of the following: get_post($missing_post_id);
    – deflime
    Nov 17, 2017 at 9:54
  • 1
    ["post_author"]=> string(1) "7" So this is author with ID 7 Nov 17, 2017 at 10:02
  • 1
    Just add 'author' => '7,8' Nov 17, 2017 at 10:03

1 Answer 1

0

You are querying for post author with ID 8 and also you are telling to match the post type to property post type and also post_status to be either publish, acf-disabled, private.

Check your post that didn't be return for the above and will found the reason.

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"
2
  • How do I enable the acf in the $args1 array?, How do I control the private value?
    – DavSev
    Nov 17, 2017 at 9:43
  • You asked to explain the Reason. So I did. To clarify the why this should be Another topic as I don't have access to the code base you use. The default wp query will check for only wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private' and not the acf-disabled status, this means that this has been extended, filtered or override. Nov 17, 2017 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.