Php wp_查询显示文章必须查看并带有快捷码

Php wp_查询显示文章必须查看并带有快捷码,php,arrays,wordpress,function,Php,Arrays,Wordpress,Function,我尝试使用此功能来显示post most视图。但有点不对劲 我还将其用于其他事情,如按类别显示帖子,在本例中效果良好 因此,可能我在new WP\u query function must_post_read() { global $post; $html = ""; $my_query_2 = new WP_Query(array( 'meta_key' => 'post_views_count', 'orderby' =>

我尝试使用此功能来显示post most视图。但有点不对劲

我还将其用于其他事情,如按类别显示帖子,在本例中效果良好

因此,可能我在
new WP\u query

function must_post_read()
{
    global $post;
    $html = "";

    $my_query_2 = new WP_Query(array(
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'posts_per_page' => 5
    ));

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

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('must_post', 'must_post_read');
函数必须\u post\u read()
{
全球$员额;
$html=“”;
$my\u query\u 2=新的WP\u查询(数组(
“meta_key”=>“post_views_count”,
'orderby'=>'meta_value_num',
“每页帖子数”=>5
));
如果($my\u query\u 2->have\u posts()):
while($my_query_2->have_posts()):$my_query_2->the_post();
$html.=“

”。获取标题()。“

”; $html.=“”。获取摘录()。“

”; $html.=”; 结束时; wp_reset_postdata(); endif; 返回$html; } 添加快捷码('must_post'、'must_post_read');

有人能帮我吗?

首先,我们需要确保post有一个自定义的
post\u meta
字段,您将在该字段上进行卖空

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($post_id) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($post_id, $count_key, TRUE);
    if ($count == '') {
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

//Short code function
function wh_must_post_read() {
    $html = '';

    $args = [
        'meta_key' => 'whpp_track_post_views', //<--  Check This
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5
    ];
    
    $my_query_2 = new WP_Query($args);

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

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('wh_must_post', 'wh_must_post_read');
在WP编辑器中

[wh_must_post]
请注意:若要查看此代码,请访问几篇文章的单个页面,以便
whpp\u set\u post\u views()
mehord将
whpp\u track\u post\u views
meta\u键添加到该文章中

以上所有代码都放在活动子主题(或主题)的function.php文件中。也可以在任何插件php文件中使用。
代码经过测试并正常工作。


相关问题:

可能
post\u views\u count
保存在DB中,前缀为
\u post\u views\u count
您确定有
post\u view\u count
meta\u键用于post,以及为什么使用
global$post?我想这里不需要它。@RaunakGupta可能需要,我没有
post\u views\u count mat\u key
,但不是wordpress设置?对于
global
yes是有影响的。不,没有wordpress设置,但是如果你想要,你可以参考我的,它可以亲手实现,但是如果你仍然面临任何问题,那么告诉我,我也会在这里发布答案。谢谢@RaunakGupta,现在更好地理解。我试着用不同的方式使用它,但都被扭曲了!对不起,我可以请你帮助理解谁写了这段代码!哇!这工作做得很好!!非常感谢你!现在我意识到这是我的错误!我真的欠你一个人情!!
[wh_must_post]