0

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

1
  • As a comment here what I have tried as well is using WP_Query to get all posts of specific type and then in the WP_Query loop check if value exists using 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
    – marwyk87
    Commented Feb 28, 2017 at 9:28

1 Answer 1

0

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
                                     )
                                    )                                           
              );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.