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

Save default value for empty or missing post meta

If you want to and only need to do what @tf suggested, what is just to take care of displaying a 0 if no value is present, you can construct a function to do so like this:

function wpse121165_return_carprice() {
    $ecpt_carprice = get_post_meta($post->ID, 'ecpt_carprice', true);
    if(! empty( $ecpt_carprice ) ){
        return $ecpt_carprice;
    } else {
        return 0;
    }
}

Use the function like this: echo wpse121165_return_carprice();

If you really need to update your database, you have to do this another way. Below code should give you an insight on how to do it:

 function wpse121165_update_carprice_meta() {
      // args to query for your key
      $args = array(
        'post_type' => 'your_post_type',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'ecpt_carprice',
                'value' => 'bogus', // you have to pass a value
                'compare' => 'NOT EXISTS'
            ),
            array(
                'key' => 'ecpt_carprice',
                'value' => ''
            )
        ),
        'fields' => 'ids'
      );
      // perform the query to get back an array of ids
      $not_exist_or_empty_ids = new WP_Query( $args );

      foreach ( $not_exist_or_empty_ids as $id ) {
          update_post_meta($id, 'ecpt_carprice', '0');
      }
 }

Use the function like this: wpse121165_update_carprice_meta();. If you put this in you functions.php and perform your meta update on the database, make sure to disable it afterwards and not to call it over and over again.

Related Posts:

  1. Search custom post type by meta data
  2. How to display liked posts of current user in wordpress?
  3. Filter query posts by the count of metas for a meta key
  4. Grouping related postmeta data via SQL query
  5. mySQL query. ORDER BY meta_key
  6. Remove posts from query for events whose start date has passed
  7. Query metas (and not : query posts by metas)
  8. Issue with using add_rewrite_rule() for post querying custom fields
  9. Query posts according to specific post meta values
  10. Order by meta value pro first then meta value free in my search function
  11. get_post_meta with WP_query
  12. Declaring a var, placing it in a query and using the output of the query?
  13. Combining two meta_values within one row with query
  14. Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
  15. Sort by postmeta on when searching
  16. Use $wpdb->get_results with filter based on array
  17. What is the best way to query posts based on live data?
  18. Custom MySQL Query for Post and Post Meta
  19. How to display SQL query that ran in query?
  20. How can I create a meta_query with an array as meta_field?
  21. Query multiple meta key values?
  22. How to Optimize WP site for millions of posts
  23. order by numeric value for meta value
  24. How to get comments by post ID?
  25. Differences between wpdb->get_results() and wpdb->query()
  26. Is there a way of increasing the speed of this query?
  27. Get all image from single page using this query
  28. How can I query all users who registered today?
  29. Get Terms by IDs with IDs order
  30. Reversing the order of posts AFTER the query is performed
  31. what are the numbers between curly brackets in search query
  32. simple sql query on wp_postmeta very slow
  33. Using WordPress public query variables
  34. How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
  35. When/why does ‘$query->get( ‘tax_query’ );’ return empty?
  36. How to get link and title of next and previous post on single page
  37. Create pagination and order according to alphabet
  38. wpdb get posts by taxonomy SQL
  39. How to List Events by Year and Month Using Advanced Custom Fields?
  40. What is the most efficient way of querying posts based on visits and date for current day?
  41. Is there any difference between hooks posts_where with posts_join and posts_search performance wise?
  42. wordpress query – orderby child post date
  43. How many WordPress SQL Queries per page?
  44. How to display Section for certain time
  45. Add multiple value to a query variable in WordPress
  46. Custom query_var causes displaying posts archive on front page
  47. Slow wp_enqueue_media()
  48. Multipart/formatted MySQL query problem
  49. Custom Queries: Joining On Meta Values From Two Custom Post Types
  50. What is the most efficient way of implementing a favorite post system?
  51. Query Custom Meta Value with Increment
  52. Remove [gallery] shortcode altogether
  53. Compare two numeric custom fields
  54. wp_dropdown_categories with multiple select
  55. Advanced Custom Fields – Query Efficiency
  56. Why does get_the_time(‘F j’) return November 30 for all posts?
  57. Query by one meta_key and sort by another (possibly NULL value)
  58. How to tune search argument in WP_Query to show only exactly the same results?
  59. Custom query filter not working on woocommerce category page
  60. How to delete a transient on post/page publish?
  61. WPDB Multiple site’s posts and get featured images
  62. Very slow query
  63. query multiple taxonomy and show post count
  64. Search Terms – Querying on either description__like OR name__like in the same Term Query?
  65. Query & Sort Comments by custom comment meta
  66. How to get my loop to pull posts into three columns
  67. How to display lastest post date in the homepage?
  68. Custom $wpdb Query for Custom Post Type by Category
  69. Get a user’s most recent post title
  70. Categories and products in random order
  71. add_query_arg() XSS Vulnerability
  72. Count user posts by user ID, Post type and Post status
  73. Can’t pass table to $wpdb->prepare
  74. Query set using tax_query with relation ‘OR’ modifies category object?
  75. $query->query_var[‘post_type’] not set for pages
  76. is_archive() doesn’t work on public query var archive pages?
  77. How to extract all ID variables from a query string?
  78. When add_query_arg() is necessary?
  79. Different Main Navigation per category
  80. How to get category link without a database query
  81. Insert html after certain amount of posts?
  82. wp remove query
  83. Show recent products first but “sold out last” in query
  84. WordPress creating excessive joins on meta_query with search
  85. How to tell if $query_var isset?
  86. Show All Posts Insert Edit Link
  87. $wpdb->get_var not returning a result
  88. search through post-type attachments titles
  89. How to count get_users query?
  90. Date query year and month OR just year
  91. cron job to auto delete posts of a specific post type older than x days
  92. Possible to get posts from multiple meta keys/values in a single query?
  93. Are database queries created using WordPress filters protected from SQL injection?
  94. Get IDs of posts currently visible on archive
  95. Get posts by category with pure SQL query
  96. can’t get query to order posts by acf datepicker
  97. Sorting Grids with Essential Grid and Events Manger
  98. $args numberposts variable
  99. The use of including upgrade.php when building custom queries
  100. Automatically adding meta data to posts or multiple query help
Categories query Tags post-meta, query
Changing site_url domain breaks image links
add_submenu_page set for multiple roles [duplicate]

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