【问题标题】:How to query to get the current custom post’s value in a single post page?如何在单个帖子页面中查询以获取当前自定义帖子的值?
【发布时间】:2021-04-07 02:49:16
【问题描述】:

我将下面的代码放在一个帖子页面中。
问题是它显示当前帖子的正确标题但计数不正确。
当实际计数为 1 时,它显示计数 0。
当前帖子已购买=yes 且 firstsubmission=yest,因此应显示 1。
我使用 'post_parent' => get_the_ID(),而不是 'title' => get_the_title() 和 'posts_per_page' => 1,但仍然显示计数 0。
你能帮我更正代码吗?

$info_args = [
    'author'          => get_current_user_id(),
    'title'           => get_the_title(),
    'posts_per_page'   => -1, 
    'post_type'       => 'infosubmission',
    'post_status'     => 'publish',
    'meta_query'      => array(
           array(
               'key' => 'purchased',
               'value' => 'yes',
               'compare' => '=',
               'key' => 'firstsubmission',
               'value' => 'yes',
               'compare' => '=',           
           )
       )
];

$info_info_posts = get_posts($info_args);
$info_info_count = count($info_info_posts);

echo 'Title: ' . get_the_title() .'<br>';
echo 'Count:' .$info_info_count ;

谢谢。

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    如果您只是简单地尝试获取属于某个自定义帖子类型的所有帖子的计数,请改用它,因为它要快得多:

    $info_info_count = wp_count_posts( 'infosubmission' )->publish;
    

    很遗憾,此功能不允许您按帖子状态以外的任何内容进行过滤,因此如果需要,则需要创建辅助查询。

    首先,您不能以这种方式按标题进行查询,因此请忽略它(无论如何,这会将您的查询限制为仅当前帖子/页面)。其次,您的 meta_query 参数对于多个字段的格式不正确。试试这个:

    $info_args = [
        'author'          => get_current_user_id(),
        'posts_per_page'   => -1, 
        'post_type'       => 'infosubmission',
        'post_status'     => 'publish',
        'meta_query'      => array(
               'relation' => 'AND',
               array(
                   'key' => 'purchased',
                   'value' => 'yes',
                   // 'compare' is '=' by default, so not needed here.
               ),
               array(
                   'key' => 'firstsubmission',
                   'value' => 'yes',          
               ),
           ),
    ];
    
    $info_info_posts = get_posts( $info_args );
    $info_info_count = count( $info_info_posts );
    
    echo 'Title: ' . get_the_title() . '<br>';
    echo 'Count: ' . $info_info_count;
    
    

    另外,像get_the_title()这样的函数默认使用全局的$post对象,所以除非你提供一个参数或者再次调用the_post(),它只会得到当前页面的标题。所以我不确定你到底想在这里做什么,但如果你想打印新查询返回的每个帖子的标题,你需要开始另一个循环遍历结果。在这种情况下,我建议创建一个新的 WP_Query 独立于主要的:

    $info_posts = new WP_Query( $info_args );
    $count_info_posts = $info_posts_query->found_posts;
    
    echo 'Count: ' . $count_info_posts . '<br>';
    
    //Start The Loop
    while ( $info_posts->have_posts() ) : $info_posts->the_post();
        echo 'Title: ' . get_the_title() .'<br>';
        // Do anything else you'd like with each post's data here.
    endwhile;
    
    // Restore original Post Data
    wp_reset_postdata();
    
    

    注意多个查询from the WordPress docs

    注意:如果您在查询中使用the_post(),则需要在之后运行wp_reset_postdata() 以使模板标签再次使用主查询的当前帖子。

    【讨论】:

    • 哦,我明白了。非常感谢您的详细解释。这是一个很大的帮助。 :)
    【解决方案2】:

    您的元查询错误,您必须为每个元键比较创建一个单独的数组。检查下面的代码。

    $info_args = [
        'author'         => get_current_user_id(),
        'title'          => get_the_title(),
        'posts_per_page' => -1, 
        'post_type'      => 'infosubmission',
        'post_status'    => 'publish',
        'meta_query'     => array(
            array(
                'key'     => 'purchased',
                'value'   => 'yes',
                'compare' => '=',
            ),
            array(       
                'key'     => 'firstsubmission',
                'value'   => 'yes',
                'compare' => '=',           
            )
        )
    ];
    
    $info_info_posts = get_posts($info_args);
    $info_info_count = count($info_info_posts);
    
    echo 'Title: ' . get_the_title() .'<br>';
    echo 'Count:' .$info_info_count ;
    

    【讨论】:

    • 感谢您的帮助 Bhautik :)
    • 我的回答有帮助吗?
    • 其实没用但是还是谢谢你们的cmets :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多