I am doing an advanced wp_query that is looking for various custom fields, based on alot of conditions. It isn’t pulling anything up… so I wanted to check that I am doing the query correctly. Below is what I am trying to check, and then the code for what I have tried …
Please let me know what I am doing wrong in my query….
1. The first checks if the banner_image2 field exists…
2. AND we check if the banner_link field exists…
3. AND we check if the banner_loc = $bannerloc (in this case, = 1)…
4. AND Then we check if the banner_cats multi select contains any items $pagecategories…
OR if banner_cats contains value of 1
5, AND then check if banner_towns = $towncatid
OR if banner_towns=1;
// These values outside of the include... $bannerloc = 1; $sizekey = 'banner_image2'; $pagecategories = wp_get_post_categories(); // get_the_category(); // Query Args inside the include... $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' => '=', ), array( 'relation' => 'OR', array( 'key' => 'banner_cats', 'value' => $pagecategories, 'compare' => 'IN', ), array( 'key' => 'banner_cats', 'value' => 1, 'compare' => '=', ), ), array( 'relation' => 'OR', array( 'key' => 'banner_towns', 'value' => $towncatid, 'compare' => '=', ), array( 'key' => 'banner_towns', 'value' => 1, 'compare' => '=', ), ), ), ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // Code for post here... endwhile; wp_reset_postdata(); endif;
banner_cats is a multiselect field. This field will contain an array of category IDs and will be stored as a serialized array. You can’t do
To match this you need to do something like this:
array( 'key' => 'banner_cats', 'value' => '"'.$cat_id.'"', 'compare' => 'LIKE', )
Notice the double quotes include in the value. This is so that 1 will only match 1 and not 10, 20.
To do something resembling an “IN” you would need to loop through the list of categories and create a “LIKE” query for each of them.
The reason that you get nothing is that the meta queries on
banner_towns are retuning nothing.
Hope that helps.
I’ve cleaned things up a bit… but now I’m getting an trim error from the meta.php file…
Here is the error..
Warning: trim() expects parameter 1 to be string, array given in /home/tipsfromtown/public_html/wp-includes/meta.php on line 1432
Since I’ve already closed this topic (cause the initial problem was fixed), and noone is responding to this thread anymore, I’ll open a new one with my code…
You must be logged in to reply to this topic.
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!
2022 was a big year for Advanced Custom Fields, with more capabilities, a new generation of ACF Blocks, a refreshed UI, and a new home. Our year in review post looks at advancements we’ve made and offers a glimpse of the future.— Advanced Custom Fields (@wp_acf) January 6, 2023
© 2023 Advanced Custom Fields.