Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Avoid multidimensional arrays –  Tom J Nowell Oct 4 '12 at 18:11

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
I am not sure how to format the 'value' field because the information I need to check is held within an array. I have tried these: 'value' => 'Drink' 'value' => 'type-of-special' 'value' => array([type-of-special]=>'Drink') but none of those work. Any idea how I should format that to get into those arrays? Thanks –  yoxalld Oct 4 '12 at 17:39
    
why would you do 'value' => 'type-of-special' 'value' => array([type-of-special]=>'Drink') ? Just access the field in the array you want, basic PHP e.g. to access item 5 in the array 'stuff' do $thefifthvalueinstuff = $stuff[5]; –  Tom J Nowell Oct 4 '12 at 18:08
    
actually looking at your code your follow up question makes no sense either. Set it to 'Drink', and you wont have to check if its type drink as only drink types will be returned –  Tom J Nowell Oct 4 '12 at 18:10
    
I think the issue is with how WCK Custom Fields Creator stores the custom fields it creates. The custom meta box is 'specials' but 'Drink' is just a piece of information held within that custom field so it isn't recognized by meta_query as a 'key'. That is why I posted the output of $special because I need help figuring out how to access the information held within those array's. Sorry for the confusion. Thanks –  yoxalld Oct 4 '12 at 19:10
    
But your meta query happens before the loop, you don't have any of that data yet so you cant use it in your query args –  Tom J Nowell Oct 4 '12 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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