Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Automatically add custom taxonomy when meta value gets to a set number or beyond

I think the best option would be is to use your getPostViews() count and incorporate a system in there to append the trending term to the post once your counter hits 150.

Here is our worksflow

  • Get the post view count (which you are already doing)

  • Compare the current count to the desired 150

  • Use wp_set_object_terms() to append the trending term to the post

IN CODE

(I have modified your code slightly. Also note, the code is untested)

function getPostViews()
{
    // Check if we are on a single page, bail out if not
    if ( !is_single() )
        return false;

    // Get the current post id
    $current_post_id = get_queried_object_id();

    $count_key = 'post_views_count';
    $count = get_post_meta( $current_post_id, $count_key, true );

    // Set up our function to set our term 'trending'
    if ( $count >= 150 ) {
        /**
         * First check if the term is not yet set, if not, set the term
         * I have used trending as term slug. Change as needed
         * Also, add the correct taxonomy to which trending term belongs to
         *
         * @link https://codex.wordpress.org/Function_Reference/has_term
         */
        if ( !has_term( 'trending', 'MY_TAX', $current_post_id ) ) {
            /**
             * Append the term trending to the post
             *
             * @link https://codex.wordpress.org/Function_Reference/wp_set_object_terms
             */
            wp_set_object_terms( 
                $current_post_id, // Current post ID
               'trending', // Slug of term to append
               'MY_TAX', // Taxonomy that the term trending belongs to
               true // VERY VERY IMPORTANT, Append the term to the other existing terms
            );
        }
    }

    if( !$count ){

        delete_post_meta( $current_post_id, $count_key );
        add_post_meta( $current_post_id, $count_key, '0' );

    }

    $views = ( $count ) ? $count . ' Views' : '0 View';
    return $views;

}

You can just simply use the code in your single page as follow

echo getPostViews();

and the function does the rest

Related Posts:

  1. Advanced search form with filters for custom taxonomies and custom fields
  2. WP_List_Table Inside Metabox Not Working on Submit
  3. Showing current taxonomy terms
  4. Custom permalinks – post type – hierarchical taxonomy’s
  5. automatically create taxonomy with same name as post title
  6. Custom Meta Boxes – Nonce Issue – Move to trash issue
  7. Display taxonomy term only if there’s a value
  8. Adding Custom Taxonomy to WordPress default Post type
  9. Show metabox in custom-post-template depending on taxonomy term?
  10. Displaying Metabox value (custom post type taxonomy)
  11. Display types of posts in edit.php instead of All, Published, Scheduled and Draft
  12. Making pages also serve as taxonomies? Or give full pages to taxonomies?
  13. get_queried_object error How to show post count by month in the taxonomy page
  14. How to filter the taxonomy terms based on another taxonomy term
  15. How can I display my custom metaboxes on a custom post template?
  16. Custom Taxonomy – Tags Metabox is showing instead of Categories
  17. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  18. Show Two custom Post type and their posts on category page
  19. Count posts with specific term_meta
  20. WordPress Orderby Numeric Value Not Working
  21. Does WordPress limit the length of slug names for Post Meta or Terms?
  22. Show the same Article Available in Other Categories
  23. Missing Posts in Custom Taxonomy List
  24. Disable block with taxonomies at post page
  25. Which post does a taxonomy term belongs to?
  26. List custom taxonomy specific to one custom post type
  27. Post image in WordPress not appearing on home page
  28. Update Post Meta for a logged in user
  29. How to duplicate entire custom post type
  30. Get related posts of child term of custom post type
  31. Two Custom Post Types Many to Many Relationship
  32. Update postmeta Parent when post_status child change
  33. Metabox not show in categories custom post type cmb2
  34. WP the_posts() on single-cars.php get category link
  35. Display different information of a custom post type
  36. Variable not working in WP_Query
  37. Filter posts by tax (dropdown) and meta value
  38. How to Display Posts From Category Within a Custom Taxonomy?
  39. how to show records that don’t have custom meta value
  40. Batch Extract Date from post title and put into ACF custom field
  41. display posts of custom post type with custom taxonomy
  42. How to query Posts from a custom post type which contains a custom taxonomy?
  43. Copy/Move selected taxonomy terms to another taxonomy for posts
  44. Different structure/content for posts from a custom post type
  45. List all posts in taxonomy term
  46. custom taxonomy listing returns no posts
  47. Making a form for user to add new custom post with custom taxonomies and custom fields
  48. best way to use custom taxonomy, post type and meta in a job system
  49. WordPress CPT Taxonomy Dashboard Search – How to include taxonomy in search?
  50. Custom Post Type meta data getting deleted on bulk editing taxonomies
  51. Should I use a custom taxonomy or custom post type for grouping a list of panels associated with a group of tests?
  52. Get_post_custom not fetching value from array wordpress
  53. Add a form in every post and save data in post meta
  54. SELECT custom post type and its meta in SQL
  55. Search CPT by tag ids
  56. Post template with breadcrumb needs to show page navigated from
  57. Save data is post using php front end
  58. What is the best way to structure posts for the same event happening in different years?
  59. Right way to store a large set of similar information on website? [duplicate]
  60. Define multiple prefixes for custom post type
  61. Show posts from WP Custom Post Type selected from a field in a metabox
  62. How to properly get the wp_postmeta.meta_value of a custom post type in specifics?
  63. I cannot display custom posts in custom taxonomy
  64. How can I group by Taxonomy on Custom Post Type while filtering out based on custom Meta
  65. How to manage a dynamic multi-level page hierearchy system?
  66. How can I get the last 5 element of this tax query?
  67. Display title of all custom posts from same taxonomy term on a custom post template
  68. Saving Child Terms on front end not setting parent
  69. Order posts by meta value hiding posts instead of re-ordering
  70. How to get all posts related to a taxonomy?
  71. update a posts of other custom post type
  72. Filter By Term Not Working – Custom Post Type
  73. Complex strcuture as CPT or taxonomy for use in woocommerce product variations [closed]
  74. Custom Post Status & Taxonomies
  75. WordPress custom post type
  76. Edit post meta direct from post.php?
  77. Post Editor: display categories from current post type taxonomy only?
  78. Values inside a custom field to determine which category posts to display
  79. How to create content automatically when a post is published?
  80. Display a post count from a custom metabox selection
  81. Linking custom taxonomies and posts
  82. How to Associate Posts with Pages
  83. Timetable of Custom Meta Data using Custom Post Type and Custom Taxonomy
  84. Query Only Show Text on Posts With Certain Taxonomy Tag
  85. Advanced search form with filters for custom taxonomies and custom fields
  86. Custom My account page and custom page for editing posts
  87. Top 30 Songs using Custom Post Type
  88. Get custom post type parent category
  89. Sort ACF by custom taxonomy
  90. How to develop Knowledge center in WordPress Website
  91. I am having a problem with fetching product data in the Gutenberg block editor
  92. ACF – Tags not showing up in Custom Post Types
  93. Hide load more button if no more post in selected category
  94. Set menu active state for custom posttype and category, given custom taxonomy term
  95. custom permalinks based on taxonomies for custom post type
  96. Archive page for taxonomy terms
  97. Custom Taxonomy Invalid in REST API
  98. How to create custom boxes with text inside?
  99. How to add post_type=value when editing that post type in the WordPress admin?
  100. How do I get array of types associated with a taxonomy?
Categories custom-post-types Tags custom-post-types, custom-taxonomy, post-meta, posts
Fetch first image URL from post and set as featured image URL
How to display post title on top of image with Genesis Featured Posts

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress