Input check to custom post type query

Question

I have a series of inputs with the custom post ID as the value.

When I write the query manually with specific ID’s it works fine and the right custom posts appear.

However, when I’ve been trying to write it so the IDs are not and the user can choose, nothing appears despite I know the ID is right.

This is the right query;

$args = array( 
'post_type' => 'house_types',
'posts_per_page' => 999,
'orderby' => 'date',
'order'   => 'ASC',
'meta_query' => array(
    'relation' => 'OR',
        array(
            'key' => 'development_plots',
            'value' => '92',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'development_plots',
            'value' => '392',
            'compare' => 'LIKE'
        )
)
);

And this is my query I’ve tried to write, which is showing nothing back;

$development_plots_query = '';
if(!empty($development_plots_choices)){
    $post_per_page = -1;
    foreach($development_plots_choices as $development_plots_query){
        $development_plots_query[] = array( 
            'key' => 'development_plots', 
            'value' => $development_plots_choices, 
            'compare' => 'LIKE' 
        );
    }
}

Which then comes into this;

$args = array(
    'post_type' => 'house_types',
    'posts_per_page'=> $post_per_page,
    'orderby' => 'date',
    'order'   => 'ASC',
    'meta_query' => array(
        'relation' => 'AND',            
        $development_plots_query
    ),
);

In terms of printing out the $args, this is the right one;

Array ( [post_type] => house_types [posts_per_page] => 999 [orderby] => date [order] => ASC [meta_query] => Array ( [relation] => OR [0] => Array ( [key] => development_plots [value] => 92 [compare] => LIKE ) [1] => Array ( [key] => development_plots [value] => 392 [compare] => LIKE ) ) ) Array ( [post_type] => house_types [posts_per_page] => 999 [orderby] => date [order] => ASC [meta_query] => Array ( [relation] => OR [0] => Array ( [key] => development_plots [value] => 92 [compare] => LIKE ) [1] => Array ( [key] => development_plots [value] => 392 [compare] => LIKE ) ) ) Array ( [post_type] => house_types [posts_per_page] => 999 [orderby] => date [order] => ASC [meta_query] => Array ( [relation] => OR [0] => Array ( [key] => development_plots [value] => 92 [compare] => LIKE ) [1] => Array ( [key] => development_plots [value] => 392 [compare] => LIKE ) ) )

Against the “broken” one;

Array ( [post_type] => house_types [posts_per_page] => -1 [orderby] => date [order] => ASC [meta_query] => Array ( [relation] => AND [0] => Array ( [0] => [1] => Array ( [key] => 0 [value] => 392 [compare] => IN ) ) [1] => Array ( [0] => [1] => Array ( [key] => 0 [value] => 392 [compare] => IN ) ) [2] => Array ( [0] => [1] => Array ( [key] => 0 [value] => 392 [compare] => IN ) ) [3] => [4] => ) )

I can see there is a massive difference, but can’t work out where I’m going wrong…

Any pointers in the right direction are helpful and hopefully, I’ve explained it all ok.

0
Sam 3 years 2019-10-24T11:54:14-05:00 0 Answers 52 views 0

Leave an answer

Browse
Browse