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

Update CPT post meta with update_post_meta and cron job

Your problem is caused by your code… Let me comment it line by line, so the problem is more visible…

global $post;  // <- great, you use global $post variable

$comp_args = array( // <- you set params for your query
    'post_type'=>'boat-company',
    'posts_per_page'=>'-1',
    'post_status '=> 'publish',

);

$posts_array = get_posts( $comp_args );  // <- you run the query

if ($posts_array) {  // you check if there are any posts
    foreach( $posts_array as $post_id ) { // and if there are any posts, you loop through them and (but not in a correct way)...

        // but this (next) line doesn't do anything... The global $post variable won't be set
        // during Cron call... So this line won't work as you want it to...
        setup_postdata($post);

        // ... and that means that you can't get ID from $post, so $company_id won't contain any ID
        $company_id = $post_id->ID;

        $company_rating = 42;

        // so this line won't work as well...
        // also it would change only those meta fields that were containing "true", so...
        update_post_meta( $company_id, 'company_rating', $company_rating, true );
    }
}

wp_reset_postdata();  // you don't have to reset query in here, because you haven't changed any global query...

So here’s the cleaner way to do what you want:

function update_company_ratings_test() {    
    $comp_args = array( 
        'post_type'=>'boat-company',
        'posts_per_page'=>'-1',
        'post_status '=> 'publish',    
        'fields' => 'ids'
    );

    $posts_array = get_posts( $comp_args );

    foreach( (array)$posts_array as $company ) {    
        $company_rating = 42;
        update_post_meta( $company->ID, 'company_rating', $company_rating );
    }
}
add_action( 'update_cron_test', 'update_company_ratings_test' );

if ( !wp_next_scheduled( 'update_cron_test' ) ) {  
    wp_schedule_event( time(), 'hourly', 'update_cron_test' );  
}  

Related Posts:

  1. what is the correct way to compare dates in a WP query_posts meta_query
  2. Get post with multiple meta keys and value
  3. Echo all meta keys of a custom-post TYPE
  4. How can I filter posts by post_parent in the admin?
  5. Post metadata deletes itself
  6. How to create a mini directory in WordPress?
  7. Search everything (posts, pages, tags, cpt, meta)
  8. Custom Post Type: Set post_title equal to a custom post type field
  9. I can’t set meta_key in my custom post type query
  10. Can’t sort order of wp_query with 2 meta keys
  11. How to get source of custom meta image?
  12. How can I include meta box content when searching?
  13. update a post meta from a single table cell TablePress
  14. Displaying Meta Box Image
  15. Automatically adding meta data to posts or multiple query help
  16. Trigger “unsaved changes” dialog for custom post meta changes
  17. Conditionally Query Custom Post Types by Post Meta for Blog Home Page?
  18. Displaying custom posts only if custom meta box’s date is not expired
  19. Displaying Metabox value (custom post type taxonomy)
  20. How do I sort a custom post type admin column using two meta keys?
  21. How can I display my custom metaboxes on a custom post template?
  22. Custom Post Type, Saving Multiple Checkboxes
  23. Create a random unique 6 digit number as custom field for custom post type
  24. Export entries and multiple custom field meta to .csv?
  25. how to interconnect custom post types?
  26. orderby in custom WP Query does not work
  27. Custom meta box data array: foreach not working correctly?
  28. Insert post metadata for all posts in CPT at once if metadata no existent
  29. Display custom post type from dynamic custom field
  30. Populate dropdown from one custom post type inside another custom post type
  31. How to check if user meta field is empty in conditional else statement
  32. Get posts between custom dates
  33. A better way to add a meta box to custom post types
  34. Cron job for creating posts not excecuting properly
  35. post meta parameter in post custom-post-type endpoint with restapi
  36. How do I set all of a particular post meta to a value within the custom post type I’m in?
  37. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  38. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  39. Custom filter function not working with Custom post type
  40. Importing Data from a Non-WordPress database, into WP
  41. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  42. Meta Data for Custom Post Type not saving
  43. Check if post with same meta value exists
  44. Filter date from post meta when date is in string format
  45. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  46. Archieve.php not loading for custom post type
  47. Create Array from custom post type to display a slider
  48. Values from meta-box not on $_POST
  49. Returning a custom content types with meta values
  50. Custom post type suddenly stoped working after WordPress 5 update
  51. copy images from custom field to another custom field
  52. Cannot obtain custom meta information
  53. is_main_query() never called on WP 4.4
  54. How can I output WPAlchemy repeating fields meta values in my page template?
  55. Save Metabox Custom Field Value
  56. Meta box data not saving
  57. Meta box with front-end styling
  58. How can I get some of a posts meta data whilst it is still inserting?
  59. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  60. Cannot Save MetaBox Data in Custom Post Type
  61. best way to use custom taxonomy, post type and meta in a job system
  62. Empty meta-box returns publishdate if no value is set?
  63. Building tags and archive using meta from custom post type
  64. How to order custom posts by one of the custom fields value, ‘date’?
  65. Custom Meta Box with variable number of fields
  66. WordPress CPT Taxonomy Dashboard Search – How to include taxonomy in search?
  67. Custom Post Type meta data getting deleted on bulk editing taxonomies
  68. How to create review point system for CPTs (many-to-many relationship)
  69. After inserting new post with wp_insert_post() the post is not visble to WP_Query, but the same WP_Query works for post inserted from wp-admin panel
  70. How can I write a function that would update any missing specific post metadata?
  71. SELECT custom post type and its meta in SQL
  72. How to keep custom post type related information
  73. Custom attachment function not working in v5.4.2?
  74. Can’t save meta box data in WordPress using custom post types
  75. How to return/export only data showing on screen in custom post type view all screen
  76. Use WP_query to match post types based on custom field values
  77. How can I use a custom template to load custom post type posts based on a post_meta value
  78. How to properly get the wp_postmeta.meta_value of a custom post type in specifics?
  79. Custom Function to redirect singular post if specific meta field is empty
  80. Stored meta from attachment, video length?
  81. Saving Child Terms on front end not setting parent
  82. Custom post type: “transition_post_status” action get title and other fields
  83. update a posts of other custom post type
  84. How do I extract the contents of a CPT’s custom field for all posts?
  85. How to get post meta for custom post type and taxonomy
  86. Delete custom post type metadata without deleting the post in admin area
  87. Order by post meta value gets random results
  88. How can I get the $key / $value pairs of custom fields that were added via 3rd party plugins or themes?
  89. How to create content automatically when a post is published?
  90. Rich Custom Field for Custom Post type not saving
  91. Add auto increment value to custom meta
  92. Return only custom post types for the page, not all
  93. Retrieve post data via WPDB class
  94. Create action running on trashed_post hook to modify post_meta value
  95. Post AND page parameter for WP function
  96. Custom Post Type meta value is being saved in array, instead of just the string (as value)
  97. How to display Author Profile based on Custom field value
  98. Check for custom field value in different post type than current one and do something
  99. I am having a problem with fetching product data in the Gutenberg block editor
  100. rest_api_init is not getting invoked inside a Class
Categories custom-post-types Tags cron, custom-post-types, post-meta
WordPress Multisite with different domains and themes
How to hide out of stock products in Related Products via custom query in WooCommerce

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