6

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!

1
  • Avoid multidimensional arrays
    – Tom J Nowell
    Commented Oct 4, 2012 at 18:11

2 Answers 2

11

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

8
  • 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
    Commented Oct 4, 2012 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
    Commented Oct 4, 2012 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
    Commented Oct 4, 2012 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
    Commented Oct 4, 2012 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
    Commented Oct 4, 2012 at 19:20
0
    $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 );

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.