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

Saving custom fields for WP_Query to retrieve

For future reference on this question. I solved my problem by updating my save routine to save each field to its own key. This does work much better since each fields is saved to its own corresponding key it makes retrieval from WP_Query much better

    /* check if this is an autosave and if YES then do nothing */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
       return;

    /* Verify the nonce field of the meta box */
    if ( !wp_verify_nonce( $_POST['propdp_details_noncename'], plugin_basename( __FILE__ ) ) )
       return;

    /* Check if the user can edit the post and if NOT the return and do nothing */
    if ( !current_user_can( 'edit_post', $post_id ) )
       return;

    /* start my array object to hold all my data to save */
    $detailsData = array();

    /* run through $_POST to check for all matching keys and values */
    foreach($_POST as $key => $data) 
    {
        if($key == 'propdp_details_noncename')
        continue;
        if(preg_match('/^propdp_details/i', $key)) 
        {
            $detailsData [$key] = $data;                
        }
    }

    /* loop through all gathered keys and values to add, update and delete meta */
    foreach($detailsData as $meta_key => $new_value)
    {

        //get old value of meta key
        $curr_value = get_post_meta( $post_id, $meta_key, true );

        //check if old value exists and add if OLD NOT EXIST
        if($new_value && '' == $curr_value)
        {
            add_post_meta( $post_id, $meta_key, $new_value, true );
        }

        //check if old value == new value and update if NEW NOT EQUAL
        elseif($new_value && $new_value != $curr_value)
        {
            update_post_meta( $post_id, $meta_key, $new_value);
        }

        //check if new value exists and delete if NEW NOT EXIST
        elseif('' == $new_value && $curr_value)
        {
            delete_post_meta( $post_id, $meta_key, $curr_value);
        }

    }

This allowed me to use my WP_Query args array like this

$myQueryArr = array(
                    'post_type' => array('custom', 'post', 'types'),
                    'post_status' => 'publish',
                    'meta_query' => array(
                                     array(
                                      'key'=>'propdp_details_listing_agent',
                                      'value'=>$currentID
                                     )
                                    )                                           
              );

Related Posts:

  1. How to show all the associated posts with specific date of data metabox?
  2. meta_query with meta values as serialize arrays
  3. Use REGEXP in WP_Query meta_query key
  4. WP_Query with checkbox meta_query
  5. Getting attachments by meta value
  6. How to grab metabox value in wp_query meta_query key
  7. What is an efficient way to query based on post_meta?
  8. WP_Query not working as expected for attachments and custom meta_query
  9. How can I create a WP_Query that returns posts where one meta_value
  10. WordPress altering my custom query, How to fix it?
  11. WP Meta Query for some meta (array) values
  12. How to get sum of meta_values of a meta_key in wp_query according to conditions
  13. Order posts by meta value and Date
  14. Get meta info related to current post
  15. How to make Meta Query case sensitive?
  16. Performance when getting post meta for post retrieved by meta value
  17. Get attachment by meta_key value
  18. Fetch Record based on meta key dates
  19. how to make members list directory through wordpress post custom meta key.
  20. Large AND OR query timing out
  21. New WP_Query loop in admin causes problems
  22. Filter posts by comparing custom meta value against postdate
  23. Nested array issue in meta_query
  24. How do I add an item to the WP admin menu?
  25. WP_Meta_Query object with conditionals
  26. Query posts by searching for a string in a meta field
  27. WP_Query meta compare must include ALL array values
  28. How do i create a custom post query when the meta value is an array?
  29. wp query to use both author id and meta_query
  30. meta_query with meta values as serialize arrays
  31. Meta query ignores multiple values of the key
  32. Trouble with serialized metadata
  33. show most viewed post
  34. Why can my filter query SOME metadata but not other metadata?
  35. Query against multiple locations within single custom post type post
  36. How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
  37. update_post_meta() not updating
  38. Query by meta value (add a dropdown of all values)
  39. WP Meta Query at depth 2
  40. WP_Query Posts by Metadata from Option Tree
  41. Return a single custom post from multiple meta queries
  42. WP_Query by meta key not returning any posts
  43. Meta_query compare operator explanation
  44. How to only display posts whose meta_value field is not empty?
  45. Nested meta_query with multiple relation keys
  46. Can wp_query return posts meta in a single request?
  47. order by numeric value for meta value
  48. Order by multiple meta key and meta value [closed]
  49. meta_query ‘compare’ => ‘IN’ not working
  50. How do I query for posts by partial meta key?
  51. WP Query Args – Title or Meta Value
  52. Meta query with string starting like pattern
  53. compare meta_query in get_posts arguments
  54. WordPress retrieving meta data for all custom post types in list view
  55. How to query posts based on lat-lng coordinate as post meta?
  56. how to show posts that are missing a meta_value
  57. Order by optional meta key?
  58. Set Alias for meta_query arguments in get_posts()
  59. Sorting: custom query with orderby meta_value_num THEN by title
  60. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  61. Display products from specific category in shop page
  62. Reduce or prevent calling of update_meta_cache
  63. Is there a way to extend WP_query so Custom Post Types can have properties?
  64. How to Compare Two Meta Fields
  65. Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
  66. Does tax_query really beats meta_query in all situations?
  67. ACF Relationship Field Search Filtering [closed]
  68. What is the most efficient way of querying posts based on visits and date for current day?
  69. Add indexing to meta_value in wp_postmeta
  70. Is it possible to orderby multiple meta_keys when using meta_value_num?
  71. Custom WP_Query order by post_meta and (author) user_meta
  72. Query WooCommerce orders where meta data does not exist
  73. Query Multiple Post types each with own meta query
  74. WP_Query displaying ALL posts
  75. Execute a large WP_Query with many “AND” Meta_Queries?
  76. Query all posts where meta value is empty
  77. meta_query where value is equal to given value
  78. WP_Query min and max values
  79. Query Custom Meta Value with Increment
  80. WP-CLI How to generate a list of posts with corresponding meta values
  81. ACF datepicker meta_query Compare Dates in m/d/Y g:i a – Not in Ymd Format
  82. WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
  83. Using custom meta_query with relation not working as expected
  84. Perform query with meta_value date
  85. querying with custom meta field with meta_query
  86. Order by two meta keys
  87. multiple meta key but get server load is very high
  88. Meta Query with date and time on the same Day before given time
  89. A WP_Query that will look for posts after 2 weeks ago OR with a certain meta value
  90. WP_Query meta_query where meta value ends in space
  91. Order by meta_value_num DESC and meta_value ASC on WP 4.0
  92. Finding all results from database within 500 miles of the given latitude and longitude [closed]
  93. Nested query inside Logical operator OR not working in meta_query
  94. Is “orderby” in WP Meta Query conflicting with Meta Query?
  95. Query by meta_key and order by meta_value_num return orderby date
  96. datetime picker, timestamps and meta queries
  97. How do I create my own nested meta_query using posts_where / posts_join?
  98. How to count post meta key values for all posts in database
  99. WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
  100. How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
Categories wp-query Tags meta-query, metabox, post-meta, wp-query
Sum root term’s entries count from child terms count
Instructure Canvas API with WordPress [closed]

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