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

how to show records that don’t have custom meta value

the most clicked item should be first and the items that haven’t been clicked yet should be last, but they are not showing up at all

They aren’t showing up because your posts query is only selecting posts that have the number_clicks meta, and thus MySQL ignores all posts which do not have that meta.

So what you need is two meta query clauses with an OR relation — the 1st clause will select posts that have the number_clicks meta, whereas the 2nd clause will select posts without that meta.

And for that, you can instead use the meta_query arg, so in your code, just replace the $query->set('meta_key', 'number_clicks' ); with:

$query->set( 'meta_query', array(
    'relation'       => 'OR',

    'number_clicks'  => array( // 1st clause
        'key' => 'number_clicks',
    ),

    'number_clicks2' => array( // 2nd clause
        'key'     => 'number_clicks',
        'compare' => 'NOT EXISTS',
        'type'    => 'NUMERIC',
    ),
) );

Then to sort the results by the number_clicks value, set the orderby to the array key (number_clicks2) for the 2nd clause above, i.e. $query->set( 'orderby', 'number_clicks2' ).

And note that the 2nd clause above uses NUMERIC as the type so that MySQL will convert the meta value to a number which then gives us the correct numerical sorting.

  • Remember that meta values are stored as strings/text in the database, hence for example (these individual values — ) 10, 4, 1, 3, 2 will be sorted into 1, 10, 2, 3, 4 instead of 1, 2, 3, 4, 10. Because that’s what happen when the values are sorted as strings.

  • So actually, you should’ve used $query->set( 'orderby', 'meta_value_num' ) so that the meta value is converted to a number prior to sorting. But now you no longer need to use that because the meta value type is already set in the 2nd clause above.

Related Posts:

  1. Advanced search form with filters for custom taxonomies and custom fields
  2. Order posts by (hierarchical custom) taxonomy terms and term children
  3. How Do I Use WP_Query to Run This Database Query as Search Result?
  4. Limiting number of custom posts shown on taxonomy page
  5. Include custom post type in “all posts”
  6. Can’t sort order of wp_query with 2 meta keys
  7. Custom Meta Boxes – Nonce Issue – Move to trash issue
  8. pre get posts changing the query
  9. Show metabox in custom-post-template depending on taxonomy term?
  10. Building an Advanced Search (text, tags, category, custom fields) – Getting the wrong SQL query
  11. Using pre_get_posts to Filter Posts
  12. Displaying Metabox value (custom post type taxonomy)
  13. How can I display my custom metaboxes on a custom post template?
  14. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  15. Count posts with specific term_meta
  16. Custom search for custom post meta with pre_get_posts interferes with WP search
  17. Does WordPress limit the length of slug names for Post Meta or Terms?
  18. How to duplicate entire custom post type
  19. Metabox not show in categories custom post type cmb2
  20. I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
  21. Display different information of a custom post type
  22. Filter posts by tax (dropdown) and meta value
  23. best way to use custom taxonomy, post type and meta in a job system
  24. WordPress CPT Taxonomy Dashboard Search – How to include taxonomy in search?
  25. Custom Post Type meta data getting deleted on bulk editing taxonomies
  26. Should I use a custom taxonomy or custom post type for grouping a list of panels associated with a group of tests?
  27. List all posts associated under custom taxonomy
  28. Saving Child Terms on front end not setting parent
  29. Order posts by meta value hiding posts instead of re-ordering
  30. Set different posts_per_page for custom post type/taxonomy
  31. What’s the most efficient way to get two queries based on an if statement?
  32. Complex strcuture as CPT or taxonomy for use in woocommerce product variations [closed]
  33. WordPress custom post type
  34. Values inside a custom field to determine which category posts to display
  35. Timetable of Custom Meta Data using Custom Post Type and Custom Taxonomy
  36. Advanced search form with filters for custom taxonomies and custom fields
  37. Automatically add custom taxonomy when meta value gets to a set number or beyond
  38. pre_get_posts causes Custom Posts appear under ‘Pages’ menu
  39. Search for portfolio tags & mixing portfoliotags and post tags
  40. How to checked selected category checkbox when my custom post inserted from frontend
  41. Allow duplicate slugs for custom post type with taxonomies
  42. list taxonomies from a custom post type
  43. How to display Custom Taxonomy under Custom Admin Menu?
  44. Display a grid of taxonomy terms at root taxonomy page
  45. WordPress custom taxonomy, posts urls
  46. Listing posts under primary and secondary taxonomies
  47. Removing custom post type slug from URL
  48. WordPress loop: Show only a Custom Post Type Taxononmy TERM
  49. post meta parameter in post custom-post-type endpoint with restapi
  50. How can I made custom taxonomies relationship?
  51. How to start a new post with custom Taxonomies already set?
  52. Nested Custom Post Type or Custom Post Type Parents?
  53. How do I set all of a particular post meta to a value within the custom post type I’m in?
  54. Customize Custom Post Type URL
  55. What’s the WP way to load remaining custom posts?
  56. How to rewrite url for any specific taxonomy?
  57. Specific query for custom post type
  58. Custom post URL 404 error
  59. limit value taxonomy based on previous taxonomy value wordpress
  60. Dynamic dropdown select values depending on other custom field value
  61. Get unique superset of taxonomy terms from a list of custom posts in another hierarchical taxonomy
  62. How do I change the custom taxonomy slug to be the same as the custom post type slug?
  63. Limit number of custom posts per taxonomy
  64. Custom taxonomies relationship
  65. I want to change the word “Tags” in WordPress to something else, but how?
  66. Permalinks /country/city/cpt/postname/
  67. Number of Custom Post Types published are not being shown in the custom page
  68. Seamless permalinks between Custom Taxonomy and Custom Posts
  69. Update postmeta Parent when post_status child change
  70. Custom post type and custom taxonomy with the same slug using the file page taxonomy only as directory
  71. What is the best practice for displaying my plugin content in themes?
  72. Categorize custom post type
  73. WordPress request fiter order by related post’s post_title
  74. How do I display the taxonomy for a custom post type in an array
  75. Pretty URL for custom search for custom post type
  76. Custom taxonomy rewrite with query var returns %2F in URL
  77. WP Rewrite Rule Issue while using Custom post type & taxonomy
  78. How to list custom taxonomies in the back end
  79. Several CPT’s with same Taxonomy name but with different Terms for each
  80. Remove slug in URL custom post type
  81. Custom Permalinks with CPT and Hierarchical Taxonomies
  82. How to conditionally redirect to the post from a taxonomy page?
  83. How do I check if the user is on a taxonomy term parent, child or grandchild page?
  84. Sort and filter custom post type posts by custom taxonomy
  85. How do I display the grand child items of a taxonomy term?
  86. How to edit this code to get the categories in achieve page?
  87. Taxonomies relations
  88. Custom post type editor with dynamic selects, one drop down populating a second second drop down not working
  89. Is it better to use WordPress Custom Post Types or Taxonomies?
  90. Get general taxonomy name for a Custom Post Type
  91. Display Custom Taxonomy Alphabetically
  92. Import data from CSV with custom taxomony empty
  93. WordPress uses url different from defined in rewrite arg of custom post type
  94. Custom Post Type Loop with Query Var
  95. Full Custom Post Type List Organised by two Taxonomies
  96. Is it possible to use have_posts function to list custom post type posts inside a category?
  97. WordPress Doesn’t Generate Taxonomy Archive
  98. URL rewriting for CTP assigned entity
  99. How to have this permalink structure: post_type/postname/custom_inner_page
  100. Suggested Post and Taxonomy structure
Categories custom-post-types Tags custom-post-types, custom-taxonomy, post-meta, pre-get-posts
Dynamically insert code to custom post type loop
Prevent posts with certain post_meta to be edited

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