WP_Query什么都不返回

时间:2014-09-04 12:10:42

标签: wordpress wp-query

我试图创造一个功能 它添加了一个短代码,用于将短代码替换为带有分页的附加文件列表。

问题是,WP_Query()并没有返回任何内容。

功能代码如下。 它在我的functions.php中,它将在我的content-page.php中调用。

$a = shortcode_atts( array( 'number' => '10'), $atts );

//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
    'post_type' => 'attachment',
    'post_parent' => get_the_ID(),
    'posts_per_page' => $a['number'],
    'paged' => $paged
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :

    while ( $the_query->have_posts() ) : $the_query->the_post();
        the_title();
    endwhile;

    $big = 999999999; // need an unlikely integer

    $retuen_string .= paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );

    wp_reset_postdata();

else :
    _e( 'Sorry, no posts matched your criteria.' );
endif;

并且print_r($ the_query)结果如下。

WP_Query Object (

[query_vars] => Array (

    [post_type] => attachment
    [post_parent] => 26
    [posts_per_page] => 15
    [paged] => 1
    [error] =>
    [m] =>
    [p] => 0
    [subpost] =>
    [subpost_id] =>
    [attachment] =>
    [attachment_id] => 0
    [name] =>
    [static] =>
    [pagename] =>
    [page_id] => 0
    [second] => 
    [minute] =>
    [hour] =>
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] =>
    [tag] =>
    [cat] =>
    [tag_id] =>
    [author] =>
    [author_name] =>
    [feed] =>
    [tb] =>
    [comments_popup] =>
    [meta_key] =>
    [meta_value] =>
    [preview] => 
    [s] =>
    [sentence] =>
    [fields] =>
    [menu_order] =>
    [category__in] => Array ( )
    [category__not_in] => Array ( )
    [category__and] => Array ( )
    [post__in] => Array ( )
    [post__not_in] => Array ( )
    [tag__in] => Array ( )
    [tag__not_in] => Array ( )
    [tag__and] => Array ( )
    [tag_slug__in] => Array ( )
    [tag_slug__and] => Array ( )
    [post_parent__in] => Array ( )
    [post_parent__not_in] => Array ( )
    [author__in] => Array ( )
    [author__not_in] => Array ( )
    [ignore_sticky_posts] =>
    [suppress_filters] =>
    [cache_results] => 1
    [update_post_term_cache] => 1
    [update_post_meta_cache] => 1
    [nopaging] =>
    [comments_per_page] => 50
    [no_found_rows] =>
    [order] => DESC

)

[tax_query] => WP_Tax_Query Object (

    [queries] => Array ( )
    [relation] => AND

)

[meta_query] => WP_Meta_Query Object (

    [queries] => Array ( )
    [relation] =>

)

[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => [is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => ab31e2fb8fd323e014706374fb98b349
[query_vars_changed] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>

[query] => Array ( 

    [post_type] => attachment
    [post_parent] => 26
    [posts_per_page] => 15
    [paged] => 1

)

[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_parent = 26 AND wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 15

[posts] => Array ( )

)

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

$a = shortcode_atts( array( 'number' => '10'), $atts );
//Protect against arbitrary paged values
$temp = $the_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page'=>'15',
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :

while ( $the_query->have_posts() ) : $the_query->the_post();
    the_title();
endwhile;

$big = 999999999; // need an unlikely integer

$retuen_string .= paginate_links( array(
            'base' => @add_query_arg('paged','%#%'),
            'format'   => '?paged=%#%',
            'current'  => $paged,
            'total'    => $the_query->max_num_pages
        ));

wp_reset_postdata();
$the_query = null; 
$the_query = $temp;
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;

答案 1 :(得分:0)

我找到了解决方案。 不得不添加'post_status'=> '继承'在$ args中。 像这样。

$args = array(
    'post_type' => 'attachment',
    'post_parent' => get_the_ID(),
    'posts_per_page'=>'15',
    'paged' => $paged,
    'post_status' => 'inherit'
);

OMG。很简单。 代码没有错。 我刚刚通过了错误的论点......