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

value of metadata is null wrong use of if statement

Try:

$rating = get_post_meta($post->ID, 'ratings_score', false); 
var_dump($rating);

You will notice that you get an array, even if your ratings_score is 2. To wit:

array(1) {
  [0]=>
  string(1) "2"
}

That will never equal 2 so your condition will never work.

The third parameter of get_post_meta() if set to true will collapse that array to a scalar, and that will work.

$rating = get_post_meta($post->ID, 'ratings_score', true); 
var_dump($rating);

Note that if your post has multiple ratings_scores you will only get one of them and it may not be the one you want.

To make this work with multiple ratings_score entries use:

$rating = get_post_meta($post->ID, 'ratings_score', false); 

if (in_array(2,$rating)) {
  echo 'found it';
}

Related Posts:

  1. How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
  2. Get ID of a page containing secondary loop in content
  3. How to add “time” data this?
  4. Next and Previous loop
  5. How to hook into container
  6. Output meta into arrays
  7. Sending mail not working correctly
  8. using update_user_meta to rank users
  9. get_post_meta printing empty fields, but it shouldn’t be
  10. Check value of post meta within IF statement
  11. Best way to check if a post with specific meta exists
  12. WP update_post_meta link loop
  13. Humanmade | Custom meta box class: How to display a repeatable meta box group?
  14. Display articles related to a custom field on a page
  15. Showing sticky posts with get_posts()
  16. single.php – how to mark current page in the loop
  17. Cannot retieve the_content() and the_author() – both returning empty strings
  18. Get post id within comments loop
  19. How to order by multiple fields using standard query_posts?
  20. Get 1 more post in loop
  21. Pagination not working in category listing [duplicate]
  22. Make loop alphabetical
  23. How to check if a product is purchased within the last 365 days before displaying something
  24. How to append a query string to pagination?
  25. For each loop on every word in post
  26. Display name of taxonomy once
  27. Use have_comments() for current post instead of last post in loop
  28. Displaying the first, second, and third posts from a category in separate slides
  29. Get the image src and href data from posts
  30. WordPress shortcode in content, output in sidebar?
  31. Using variable in WordPress loop as criteria
  32. WordPress Alphabetical Sort Loop Issue
  33. Get_template_part() problem with the_content()
  34. Frontpage pagination by week
  35. Integrating WordPress with Your Website
  36. Multiple instances of Featured Image Thumbnail As A Background On Homepage
  37. Altering “posts_per_page” for defaut loop
  38. Why doesn’t ‘continue’ work in page?
  39. Fetching posts from wordpress function in ajax
  40. Remember the Meta Value With Radio Buttons
  41. Saving return value from the_author_posts_link()
  42. Accessing the Posts page Content
  43. how to run loop in function.php that sends email based on specific conditions?
  44. Query post & loop problem.
  45. Exclude current post ID from loop in sidepbar.php
  46. loop inside a loop : search for posts in the same category
  47. How to sort a loop after most viewed
  48. How do i create a loop that list and divide posts into months?
  49. Forcing page to top of the list when using get_pages
  50. How to declare a variable in a loop and make it available in the template file
  51. WordPress posts loop not displaying all posts on blog
  52. get_page_by_title not working inside fucntions.php
  53. How to use single.php output in function.php
  54. Duplicate posts on archive page
  55. Bootstrap tabs are not being clicked in WordPress loop
  56. Get gallery in loop through ajax
  57. Get a specific size from wp_get_attachment_image_src
  58. Loop random posts and display odd and even based on two alternating meta values?
  59. WordPress + Isotope: how to give different widths to each entry?
  60. hide woocommerce tab if empty
  61. Get top Page IDs from menu and cycle through their child pages on a scroller
  62. How to manually define the primary loop
  63. Url outside the loop
  64. How to filter get previous post function by meta value DESC and post date DESC?
  65. Show other category posts in single.php
  66. Why does it loop twice?
  67. Cant pull in featured Image
  68. Custom Looping of WordPress Posts
  69. how to place Post and page content side by side
  70. Add specific class to featured posts
  71. Get meta value when the page is a blog archive
  72. Echo Most Recent Sticky Post in Loop?
  73. Link Button url count php and wordpress
  74. ACF field not appearing correctly in loop
  75. Posts are being displayed from old to new, in ascending order
  76. Loop in taxonomy for terms and post
  77. WordPress Tags in class
  78. Set loop format & have a loop inside other one
  79. Need a conditional to test if title of parent page matches title of child page
  80. Query counting excluded category on paged loop
  81. Using shortcode within shortcode with dynamic variable
  82. modify default HAVE_POSTS() Loop without inserting new variable (i.e. $loop->HAVE_POSTS)
  83. Why is my pagination showing up?
  84. Changing the loop w/o killing category links
  85. Custom loop page with post navigation
  86. Pages with a loop (index, archive) are loading the first image as post_thumbnail
  87. the loop – how to control whether wp or plug-in runs it
  88. Randomly Display One Image From a 10-Image For Loop
  89. An action that runs after each post in the loop on index/archive page?
  90. Modify my code – which takes the first sentence of the post and use it as a h2 tag – to work outside the loop
  91. How to show only specific tag in wordpress loop
  92. Getting post id in for loop
  93. Different style for first two (sticky) posts
  94. WooCommerce – Checkout suddenly stops working [closed]
  95. WordPress loop put title into variable
  96. How do I display 3 post each in a bootstrap carousel?
  97. Multiple loops using next_posts_link & previous_posts_link
  98. Want to images load first then title in WordPress loop
  99. How to add a continuous number to HTML tag attribute value inside The Loop [closed]
  100. WordPress Not Sorting By Custom Field
Categories loop Tags loop, post-meta
How to customize custom taxonomy url?
Using fetch_feed to retrieve items with non common titles

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