0

I've been trying to change the code of my WordPress site. Rather than showing "trending posts" which is determined by post_views_count, I simply want to show posts from a certain category. The code, as it stands is:

if ( !function_exists( 'getCrunchifyPostViews' ) ) {
function getCrunchifyPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
  }
}

if ( !function_exists( 'setCrunchifyPostViews' ) ) {
function setCrunchifyPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
  }
}

How can I simply get it to show posts of a certain category? (FYI the category_name is 'review')

New contributor
SS1986 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • is this not possible with a post query block?
    – Tom J Nowell
    May 13 at 16:39
  • Thanks @TomJNowell - you're talking to a complete novice here, buddy! Can you point me in the direction to any decent resources where I can pick up some advice on post query blocks?
    – SS1986
    May 16 at 9:34
  • post query blocks aren't a coding/PHP feature, you can find them in the block inserter alongside paragraphs lists embeds etc. The official handbook docs on wp.org should cover these. They're also a part of the latest default theme twenty twenty two
    – Tom J Nowell
    May 16 at 9:39
  • also, you shared the code for getting/setting view counts but no information about the code that displays trending posts itself
    – Tom J Nowell
    May 16 at 9:40

0

Your Answer

SS1986 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.