get_post_meta in WP_Query

时间:2014-05-26 作者:user51590

我正在做一个插件来添加喜欢的帖子

我想在插件中的页面上创建一个表,以显示每个帖子和喜欢的数量

function my_plugin_options() {

    echo \'<p>Table of Likes</p>\';
    echo \'</div>\';

    echo \'<table>\';
  echo \'<tr>\';
  echo \'<td>Post</td>\';
  echo \'<td>Number of likes</td>\';
  echo \'</tr>\';

    // -- WP_Query -- 

    $like_args = array(
        \'post_type\' => \'post\',
        \'order\' => \'DES\',
        \'post_status\' => \'publish\'
    );

    $like_loop = new WP_Query($like_args);

    if($like_loop->have_posts()):
        while($like_loop->have_posts()):
            $like_loop->the_post();

    // -- WP_Query -- 


    $likes = get_post_meta( $like_loop->post_ID, "_like_amount", true);     

    echo \'<tr>\';
    echo \'<td>\';
    the_title();
    echo \'</td>\';
    echo \'<td>\';
    $likes;
    echo \'</td>\';
    echo \'</tr>\';

    endwhile;
    endif;

    wp_reset_postdata();

    echo \'</table>\';
}
我在每个帖子的“\\u like\\u amount”中都有一个元字段,即赞数。

如何使用get\\u post\\u meta检索WP_Query

我试过了

$likes = get_post_meta( $like_loop->post_ID, "_like_amount", true);
但它失败了

3 个回复
SO网友:Privateer

如果你想发很多帖子(或者只是想减少查询次数),我建议你重新写一点。

首先,构建一个过滤器函数来修改您将要进行的查询。

function filter_query_add_likes( $clauses, WP_Query $wp_query ) {
    if ( !is_admin() && !$wp_query->is_main_query() ) {
        global $wpdb;

        # Add your wanted meta value in as times_liked
        $clauses[\'fields\'] .= \', IFNULL( meta_like.meta_value, 0 ) as times_liked \';

        # Join the postmeta field based on the post id and the key wanted
        # - this assumes you only have one times_liked meta for each post id!
        $clauses[\'join\'] .= <<<SQL
 LEFT JOIN {$wpdb->prefix}postmeta AS meta_like ON meta_like.post_id = {$wpdb->prefix}posts.ID AND meta_like.meta_key = \'_like_amount\'
SQL;

    }
    return $clauses;
}
接下来,在运行查询之前使用该过滤器,以便在每篇帖子中返回您的喜欢字段。

function my_plugin_options() {
    # Same as what you did, just using heredoc
    echo <<<HTML
<p>Table of Likes</p>
</div>
<table>
<tr>
    <td>Post</td>
    <td>Number of Likes</td>
</tr>
HTML;


    $like_args = array(
        \'post_type\' => \'post\',
        \'order\' => \'DES\',
        \'post_status\' => \'publish\'
    );
    # Set our new filter query to apply for this query
    add_filter(\'posts_clauses\', \'filter_query_add_likes\', 10, 2);

    $like_loop = new WP_Query( $like_args );

    # Remove our filter query to avoid touching other queries on accident
    remove_filter(\'posts_clauses\', \'filter_query_add_likes\', 10, 2);

    if ( $like_loop->have_posts() ) {
        while ( $like_loop->have_posts() ) {
            # using next_post like this pulls your posts out for easy access
            $current_post = $like_loop->next_post();

            # your times_like (from the filter) can now be accessed without
            # having to do an extra query with get_post_meta()
            $likes = $current_post->times_liked;
            # your title can be pulled right in if wanted
            $title = $current_post->post_title;

            # Draw your column
            echo <<<HTML
<tr>
    <td>{$title}</td>
    <td>{$likes}</td>
</tr>
HTML;

        }
    }

    # End the table
    echo "\\r\\n</table>";

}
这可以做几件事:

现在,您只需使用一个查询就可以拉入所有要显示的结果,您可以轻松地从post对象中拉入任何需要的内容,包括喜欢该帖子的次数总的来说,比使用get\\u post\\u meta要快得多,尤其是在处理大量帖子的情况下。

SO网友:s_ha_dum

一旦你跑步$like_loop->the_post() 全球$post 变量设置为循环中的当前post,因此您需要:

$likes = get_post_meta( $post->ID, "_like_amount", true); 
或。。。

$likes = get_post_meta( get_the_ID(), "_like_amount", true); 
。。。在某些情况下,它会更加健壮。

然而,我不知道你所说的“如何在我的WP_Query“。作为示例提供的代码与WP_Query 如果要在查询参数中包含该值,则不需要解释要查询的条件。

SO网友:Tom J Nowell

原始海报表明这是解决方案:

global $post;

$likes = get_post_meta( $post->ID, "_like_amount", true);
问题是获取当前帖子ID

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post