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

Automatically adding meta data to posts or multiple query help

This will query all the ticket posts and update a _ticket_date field. However, I’m not yet sure how to retrieve the actual date, so you’ll have to provide me with a little more information for that. Assuming it works properly I would run the following once, and then remove. You could achieve a similar effect by using setting a transient but I won’t get into that now.

function update_ticket_meta(){

  $args = array( 'post_type' => 'ticket', 'nopagination' => true );
  $tickets = new WP_Query( $args );

  if ( $tickets->have_posts() ) while( $tickets->have_posts() ){
    $tickets->the_post();

    // get the event ID- adjust meta key as needed
    $event_id = get_post_meta( $post->ID, '_event', true );

    // get the date from the event post
    $event = Ai1ec_Events_Helper::get_event($event_id);

    if( $event ) {
       // get date in desired format- YYYYMMDD should be the easiest to query by
       $date = date( 'ymd', $event->start );

       if( $date )
        update_post_meta( $post->ID, '_ticket_date', $date );
    }

  }
}
add_action( 'admin_init', 'update_ticket_meta' );

Additionally, to save the meta whenever a ticket is saved you can add an action to save_post-{post-type}. This will still leave you some coding to do if you change the event’s date meta field.

// save some meta when a ticket is saved
function add_ticket_date( $post_id ){

    if( !class_exists( 'Ai1ec_Events_Helper' ) )
          return;

    // get the event ID- adjust meta key as needed
    $event_id = isset( $_POST['_event'] ) ? (int) $_POST['_event'] : -1;

    // get the date from the event post- again adjust meta key
    $event = Ai1ec_Events_Helper::get_event($post->ID);

    // get date in desired format- YYYYMMDD should be the easiest to query by
    $date = date( 'ymd', $event->start );

    if( $date )
        update_post_meta( $post_id, '_ticket_date', $date );


}
add_action( 'save_post-ticket', 'add_ticket_date' );

Related Posts:

  1. Custom Queries: Joining On Meta Values From Two Custom Post Types
  2. WordPress Orderby Numeric Value Not Working
  3. My theme saves their custom post type’s metadata as a serialized array, how to access the keys?
  4. Display post content with respect to its title?
  5. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  6. Query Custom Post Types with checkboxes
  7. Create Array from custom post type to display a slider
  8. Returning a custom content types with meta values
  9. is_main_query() never called on WP 4.4
  10. Getting custom post types to properly display alphabetically
  11. Linking posts together with Advanced Custom Fields “both ways”
  12. meta query always fails even if data is correct
  13. Retrieve post data via WPDB class
  14. Update database from Quick Edit according to Checkbox state of Custom Post Type
  15. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  16. First custom field value (out of several) displayed twice after query
  17. Dynamic Custom Fields
  18. Prioritize posts in query by meta keys?
  19. Show Custom Post Type meta boxes only on Page Edit
  20. What’s the equivalent of Hide From Menus on MODX on WordPress?
  21. Custom filter function not working with Custom post type
  22. WP_Query orderby and tax_query
  23. Limit the post for differents custom post type in the same wp_query
  24. I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
  25. Displaying Posts from Custom Post Types
  26. Different Ways to Query Custom Post Types?
  27. Importing Data from a Non-WordPress database, into WP
  28. Getting thumbnails of a custom posts featured images in a page-templates metabox
  29. How to organise this data within WordPress
  30. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  31. Remove Post Custom Meta Box
  32. Can’t publish custom post type – “You are not allowed to edit this post.”
  33. Custom post type posts don’t show in archive widget
  34. Custom fields for custom post type
  35. Show future events based on custom field AND order by date
  36. Showing specific post in order of array wp_query
  37. Meta Data for Custom Post Type not saving
  38. Using preg_replace to filter custom textarea
  39. Check if post with same meta value exists
  40. Filter date from post meta when date is in string format
  41. Get only used meta_values
  42. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  43. Custom Post Types — $args vs. labels array
  44. Is there a way to exclude posts based on meta_values?
  45. Number of posts in the archive
  46. Query of custom post types only shows first post
  47. Custom query for custom post_type
  48. Adding custom field values to wp_list_pages
  49. Query multiple taxonomies with pre get posts
  50. How to query 2 custom post types that need to share a slug?
  51. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  52. Get an array wich contains the post_name of every post that has the custom post_type “pelicula”
  53. Archieve.php not loading for custom post type
  54. Add first letter of titles to array, then compare arrays
  55. Submitting Custom Post Types with custom fields from Front-end form
  56. create custom meta box with default value
  57. Get $post->ID from a URL
  58. WP_Query() with custom post type and taxonomy — get all terms?
  59. Displaying a div from an assigned meta_value when on a page
  60. Meta data (Tags and Categories) for Custom Posts not showing.
  61. Display different information of a custom post type
  62. show all custom posts types in home page
  63. Setting Post Title via Meta Data returns Auto-draft
  64. How to Echo Metadata Value in Currency Format
  65. Issue on Working with Metabox – Checkbox to enable Sale Items
  66. Filter posts by tax (dropdown) and meta value
  67. delete_post_meta() for whole CPT / multiple posts?
  68. List of child custom post types lists all custom post types
  69. How to get specific post meta by title or id
  70. Publish and save specific postmeta to a filtered post_type
  71. Query custom post type based on post id and custom field value
  72. Listing custom post types on archive page with array
  73. How to move a post to different post type with all meta data?
  74. Make (custom) post type accessible only by custom query
  75. Why do I have to press the “Update” button twice to save my meta box values?
  76. Inefficient Query Confusion
  77. Querying multiple values from a single key
  78. Can I query posts by taxonomy conditionally based on post type?
  79. How can I display a drop-down select of Post Names
  80. Check if user has comment on current post
  81. How to conditionally add Custom Post Type to Front Page
  82. how to show records that don’t have custom meta value
  83. Graphql: Unable to query (where:{author}) for custom post type
  84. WP API Response does not show my registered metadata
  85. How to detect that the save_post hook is calling the callback associated to the current edit post page only
  86. $post->post_meta not pulling any post in wordpress/php
  87. Values from meta-box not on $_POST
  88. WP_Query of custom post type sorted by meta_key has unexpected results
  89. Query not work for current taxonomy
  90. Display a post from custom post type only if all the selected taxonomies and custom field value matches the record
  91. Custom post type suddenly stoped working after WordPress 5 update
  92. Batch Extract Date from post title and put into ACF custom field
  93. copy images from custom field to another custom field
  94. Custom Query: If One Post Object Field Value Is The Same As Another
  95. Break up posts per page in two sections
  96. Custom Post Type not showing in main loop
  97. get_terms() returns Trying to get property of non-object error for custom taxonomy
  98. Custom Post Type setup
  99. How to show custom field on specific custom post type posts only when filled
  100. How to handle this specific case of custom post type?
Categories custom-post-types Tags array, custom-post-types, post-meta, query
How to stop PHP code running when in a child theme
How can I check if the admin bar is visible to the current user?

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