Thanks in advance for any help offered. I am writing a plugin for wordpress that has multiple custom post types sharing some custom meta boxes and fields. I have everything working and saving correctly but have run into a problem where I need to retrieve filtered data with WP_Query based on values in the custom fields.
Following some examples online and referencing the codex I have the following
My custom meta box code below
<label>Listing Type</label>
<select id="details_listing_type" name="details_listing_type" value="'.$detailsData["details_listing_type"].'">
In My Save Routine Im doing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST['details_noncename'], plugin_basename( __FILE__ ) ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$detailsData = array();
foreach($_POST as $key => $data)
{
if($key == 'details_noncename')
continue;
if(preg_match('/^details_listing/i', $key))
{
$detailsData [$key] = $data;
}
}
update_post_meta($post_id, 'details_listing', $detailsData );
return $detailsData ;
From here I have gathered that I am saving my custom field data in with a single key and then the array is serialized as the value. I have multiple fields but in the example above only one.
Now I want to use WP_Query to filter my posts by the value of 'details_listing_type' but form what I can understand is that since it is serialized I need to use custom Query for this.
Reason I want to use WP_Query is that I understand how it works and I am already using it in the plugin most places to retrieve data. Also I want to build a really advanced search box for users to filter data using any field and I am comfortable with WP_Query to do this.
Can someone explain how I can my code so I can still use WP_Query and store each field with its own key?
Thanks again for the help
get_post_meta()
and building an array for displaying posts later. I am hoping there is a better solution out there than this. Hopefully I can just save my data differently and retrieve using WP_Query