Question

Ok, my question is a bit complex so I will try and give a general explanation and then go into details afterward.

I am running WordPress 3.4.2 and I am using WCK Post Type Creator and WCK Custom Fields Creator to create my custom post type and custom fields.

I am trying to query posts from the custom post type 'business' that have a custom field 'special' attached to it. My problem is that I would like to be more specific and only get a certain type of special ( which is set in the Custom Fields Creator plugin ) but I don't know if/how I can do this using meta_query argument for WP_Query.

What I have now will pull posts from the post type but I then have to use get_post_meta() to find the custom fields and loop through them doing tests for what I want. Here is what I have right now:

<?php   
$args = array(
    'post_type' => 'business',
    'meta_query'  => array(
                array(
                    'key' => 'specials'
                )
            )

    );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); 

$specials = get_post_meta( $post->ID, 'specials'); 

//Loop through array    of specials
foreach( $specials as $special){

    //Loop through each special
    foreach($special as $individual){

    //Check to see is there is a drink special
    if ($individual ['type-of-special'] == 'Drink' ) { 
        //Do stuff          

    }//indivdual
    }//special
} //specials

endwhile; ?>

So, what I am doing here is pulling the posts and then for each post getting the 'specials' meta data and looping through the different arrays to check what kind of special it is and then displaying stuff if it is the right kind of special. Is there a way to be more specific in my WP_query so I only return the posts that are of a certain type of special?

The problem I seem to be running into is that I don't know how to dig down far enough into the multidimensional arrays that the 'specials' custom field has to check for values with the meta_query argument for WP_Query.

If I print $special in the foreach($specials as $special) I get this:

Array
(
    [0] => Array
        (
            [type-of-special] => Drink
            [name-of-special] => Pumpkin Latte
            [price] => 2.00
            [day-of-the-week] => Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
        )
)

So I don't know how to get to [type-of-special] key to check its value through meta_query.

I know this is a lot of information and I really appreciate any feedback and help to solve this issue. If you need more information or clarification on something please let me know. Thanks again!

Was it helpful?

Solution

Normally you would just specify a meta_value, e.g.:

 $args = array(
     'post_type' => 'business',
     'meta_query'  => array(
            array(
                'key' => 'specials',
                'value' => 'these are not the specials you are looking for - Obi Wan Kenobi'
            )
        )

);

However because your post meta is actually a data structure, a serialised PHP array/object, that's not possible.

You can read more here:

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

OTHER TIPS

    $args = array(
        'post_type' => "your_post_type",
        'meta_key' => 'name_of_the_meta_key',
        'meta_compare' => '=', // see documentation for compare options
        'meta_value ' => 'the_value_you_want to search'
    );

    $posts = new WP_Query( $args );

https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

if you want so search for items that are not empty

    $args = array(
        'post_type' => "your_post_type",
        'meta_key' => 'name_of_the_meta_key',
        'meta_compare' => '!=',
        'meta_value ' => ''
    );

    $posts = new WP_Query( $args );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top