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

Get Current User Id Inside a Loop Returns 0 For a Shortcode

You have added get_current_user_id(); in single quote. please remove quote from 'get_current_user_id()';.

function get_meta_value_by_meta_key(){

   $author_id = get_current_user_id();
   // do stuff to get user I
   $author_posts = get_posts( array('author' => $author_id, 'numberposts' => -1 )); 
    // needed to collect the total sum of views
   $counter = 0; 
   // needed to collect the total sum of views 
   echo '<h3>Post views by Author:</h3><ul>'; 
    // do stuff to get author name
    foreach ( $author_posts as $post ) { 
       $views = absint( get_post_meta( $post->ID, 'Creation_Views', true ) ); 
       $counter += $views; 
       echo "<li>{$post->post_title} ({$views})</li>"; 
    } 
    echo "</ul><hr /><p>Total Number of views: <strong>{$counter}</strong></p>";
 }
 add_shortcode('Stats', 'get_meta_value_by_meta_key');

Related Posts:

  1. How to display multiple Post meta_key/meta_values by SQL query
  2. Use WP_Query in shortcode
  3. Random order of WP_Query results with highest meta value
  4. Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
  5. WP_Query adds “(wp_posts.ID = ‘0’)” so no results are returned
  6. Archive post by meta value + 24hours
  7. Is it possible to retrieve all posts with a certain value for metadata?
  8. Order a WP_Query by meta value where the value is an array
  9. WordPress SQL JOIN query
  10. WP_Query: How to get results from both meta_key options?
  11. get_terms with specific id order
  12. get different meta-data of a complicated query at the same time
  13. Delete post meta by serialized meta value
  14. WordPress search query, how to modify the sql
  15. Efficient way of querying for a “fallback” post?
  16. SQL Query Search page
  17. direct query to post_meta table
  18. How to sort search result by post_title, then by post_content
  19. How Can I Call Up The Author’s Name on a Post
  20. phpMyAdmin displays error when importing database
  21. have_posts() execution failure
  22. Preferred Method of debugging a wordpress SQL calls?
  23. How to filter posts by post format “standard” from wp-json api?
  24. Why is variable not working on custom sql query using wpdb?
  25. How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
  26. Can’t search posts using WP_QUERY inside AJAX Function
  27. How to query for pages/post depending on slug?
  28. How can I use wp_query to show all product data using just the products ID?
  29. Retrieving Author ID in wp-admin area
  30. Get stock by custom meta field on all Woocommerce variable products
  31. Custom page archive query by url var calling page twice on one of my custom queries?
  32. WordPress custom slug (endpoint) and compare all links
  33. How to query for posts with a null or blank post_name?
  34. WordPress pagination not working with search page
  35. get current custom post ID by WP_Query method
  36. Transferring working local PHP site to wordpress – with database (MySQL)
  37. how to delete 30 day old data using PHP [closed]
  38. Display fields as values in array from external SQL DB
  39. How to securely provide a $_POST var in WP_Query with PHP 7?
  40. Using foreach loop breaks
  41. The text box have space character
  42. how to remove metadata from the posts of my blog?
  43. How can I create a list of page titles from custom meta values?
  44. Trying to remove post thumbnail with plugin
  45. Randomly Assign an Image’s Alt Text Based on Data From Post
  46. Create WP_Query to search for posts by their categories or their parent/child categories
  47. WP Query with custom Shortcode
  48. Storing Array from returned database query and using the array in a new query
  49. wp_query with cat not working
  50. How to use an associative array in post__in with WP_Query?
  51. Getting the author name on author archive page
  52. If Query In Sidebar
  53. Recent Posts slider, using WP_Query(), loads duplicate sliders
  54. Compare post-IDs within WP_Query? (Less than / Greater than)
  55. Importing Geo data into wordpress database
  56. Detect session/cookie variable in wordpress to prevent access to documents
  57. How to use $query->set with post__in?
  58. Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
  59. Sort custom meta column by other meta value
  60. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  61. Loading the same WP_Query in two different wordpress .php templates
  62. How to prevent WP_Query function from returning all posts when empty?
  63. Querying wpdb using PHP
  64. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  65. Dynamic content based on a URL parameter
  66. Display a specific category of products in shop page and disable code for specific actions
  67. Creating user status mode in WordPress
  68. Need help with Google drive API [closed]
  69. Add more button if more 8 items
  70. How to store post ID’s in cookie or session to display the same posts later
  71. WordPress, AJAX and pre_get_posts using conditional tags
  72. Get users that likes the post
  73. get_posts() and WP_query limits ‘AND’ conditions to a maximum of 6 for meta value queries in WordPress
  74. Exclude posts based on meta value
  75. Recent Posts Not Showing Only On A Specific Category Page [closed]
  76. edit_comment_link is not showing for comment author
  77. Custom query, checking values of multiple meta keys
  78. auto-populating custom nav with all items from custom post type
  79. inserting a post from an extern php file but post content doesn’t show on wp site
  80. WordPress update_post_meta updating with empty meta_value field [closed]
  81. Remove echo from shortcode
  82. Genesis framework comments broken?
  83. How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
  84. $wpdb->insert() does not Insert record in a table
  85. Databases – Submitting data from inputs to database [closed]
  86. WordPress theme options error
  87. Remove author link wherever author’s name is display
  88. Security for data obtained from the database
  89. Fatal error: Call to a member function query() on a non-object
  90. Get posts from multiple post type
  91. Get post id within comments loop
  92. How to get all author posts outside of author templates
  93. WP_Query fails despite having 1 post
  94. Create a Blog Template Page
  95. Non-Closing PHP Query in WordPress Loop
  96. unable to use ‘new WP_Query’ in AJAX call
  97. wp_query get the 2nd post
  98. Exclude posts with specific metadata from search?
  99. Is it best to avoid using $wpdb for security issues?
  100. How to get post ID in a Page?
Categories PHP Tags author, php, post-meta, sql, wp-query
How to avoid manage posts screen duplicates when two plugins use the same library
How to check if do_shortcode will be execute directly in a template php file

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