Home › Forums › General Issues › WP_Query … condition statements … now getting trim errors…
I have a long list of conditional statements in my query, looking for fields containing certain elements… I had some trouble with getting the query to work at all, but that was cleared up with this forum’s help. However, now that the query is pulling results, I’m getting a trim error from the meta.php file…
Here is the error (and it is occurring somewhere within the keys: banner_locs, banner_cats, and banner_towns … when I remove those parts from the query, there is no error)
Warning: trim() expects parameter 1 to be string, array given in /home/mysite/public_html/wp-includes/meta.php on line 1432
and here is my code…
<?php
$pagecats = get_the_category($post->ID); // wp_get_post_categories(); //
foreach ($pagecats as $catz) {
$pagecategories[] = $catz->term_taxonomy_id;
}
$pagecategories[] = '1'; // include "all cats" acf value
$currenttowns = array();
$currenttowns[] = '1'; // include "all towns" acf value
if($towncatid!=0 || $towncatid!='') {
$currenttowns[] = $towncatid;
}
?><!-- CURRENT CATS AND ALL : <?php print_r($pagecategories); ?>--><?php
// Prints a straight array with 2 elements ...
?><!-- CURRENT TOWNS and ALL : <?php print_r($currenttowns); ?>--><?php
// Prints a straight array with 2 elements ...
?><!-- LOCATION : <?php echo $bannerloc; ?>--><?php
// Prints the value '1'
$sizekey = "'banner_image2'"; // Is this correct??? See sizekey below...
// Query Args
$args = array (
'post_type' => 'post',
'orderby' => 'rand',
'cat' => 2231,
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $sizekey,
'value' => '',
'compare' => '!=',
),
array(
'key' => 'banner_link',
'value' => '',
'compare' => '!=',
),
array(
'key' => 'banner_loc',
'value' => $bannerloc,
'compare' => 'LIKE',
),
array(
'key' => 'banner_cats',
'value' => $pagecategories,
'compare' => 'LIKE',
),
array(
'key' => 'banner_towns',
'value' => $currenttowns,
'compare' => 'LIKE',
),
),
);
// then if while statements... for loop
?>
That’s odd. It seems that WP is saying that it’s seeing one of those fields as an array.
Can you do some testing, just do one of the meta queries at a time and figure out which one is causing the error?
The two trim errors are coming up when the two keys are involved (banner_cats and banner_towns). So, if I remove one of those arrays from the query, only one trim error occurs. Both of those keys are multi select fields in acf.
When I print the $pagecategories array (to check it), I get…
Array
(
[0] => 1
)
When I print $currenttowns array, I get…
Array
(
[0] => 1
[1] => 3215
)
So, I have one array (i.e. $currenttowns) comparing to the select field array (i.e. banner_towns) … can I not do that? I need to see if any of the fields in the arrays match, if so, then the query should call that post.
I’ve found a “round-a-bout” way to do this… I run the query without the banner_cats and banner_towns conditions (and get rid of the posts_per_page). This will pull alot more results… Then, during the while loop, I check each post for an array_intersect for each meta keys. Since I only want 1 result, I break the loop if one is found.
While the solution works, I’d prefer to do this during the query process (to save resources)
This is extremely odd, when you try to use “IN” WP does not detect that there is an array stored in the field, it’s looking for multiple meta value rows in the database. When you attempt to have WP treat it as a string with “LIKE” it then decides to detect that there is an array (serialized) stored in the field.
WP has been doing some work on the meta queries to allow for the new clauses for meta_query https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
Try forcing the field type to “CHAR”
array(
'key' => 'banner_cats',
'value' => $pagecategories,
'compare' => 'LIKE',
'type' => 'CHAR',
),
The topic ‘WP_Query … condition statements … now getting trim errors…’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.