Home › Forums › General Issues › Trouble filter query on acf checkbox value and $paged
Hi there,
So, I’m trying to run a custom archive query on a custom post type, but I only want post items that have a specific checkbox selected to appear. The trick is that I also need $paged to work along with it. The following code results in nothing at all.
Here’s my code.
$selected = get_field('display_where');
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $selected,
'value' => 'speight architecture',
'compare' => 'LIKE'
)
)
);
$paged = $args->query_vars['paged'] < 1 ? 1 : $args->query_vars['paged'];
$first = ( ( $paged - 1 ) * $args->query_vars['posts_per_page'] ) + 1;
$last = $first + $args->post_count - 1; ?>
<p class=results-count>Showing <?=$first?> - <?=$last?> of <?=$query->found_posts?> home plans.</p>
<?php $query = new WP_Query( $args );
if ($query->have_posts()) :
while ( have_posts() ) : the_post();
$selected = get_field('display_where');
if( in_array('speight architecture', $selected) ) {
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
endwhile;
wp_reset_postdata();
endif; ?>
The following code results in only the right items appearing, but the pagination showing all posts, regardless of how they’re being queried:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_posts = $wp_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);
$selected = get_field('display_where');
$args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => $paged,
);
echo "<p class=results-count>Showing $start_post - $end_post of $total_posts home plans.</p>";
$query = new WP_Query( $args );
if ($query->have_posts()) :
while ( have_posts() ) : the_post();
$selected = get_field('display_where');
if( in_array('speight architecture', $selected) ) {
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
endwhile;
wp_reset_postdata();
endif; ?>
(Note: Please pardon in advance if I’m missing something so ridiculously obvious here. I’m on about my 46th hour of insomnia and my head’s not entirely right)
I’m not sure about the field group setup you have, but I believe you need to set the “key” option with the field name like this:
array(
'key' => 'custom_field_name',
'value' => 'speight architecture',
'compare' => 'LIKE'
)
Please change the “custom_field_name” with the field name you have.
Hope this helps.
I tried that. It doesn’t work. It still outputs all items in the custom post type instead of only the items that meet that particular condition.
btw, the field group is a checkbox – not yes/no. They have specific selections: “speight” & “embassy”. I need to only display items that have “speight” selected.
I can get the right plans to display properly by using the following code, but it doesn’t fix the $paged issue (it still shows the count of *all* items in that post-type instead of just the ones in my query args.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_posts = $wp_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);
$args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => $paged
);
$query = new WP_Query( $args );
if ($query->have_posts()) :
while ( have_posts() ) : the_post();
$selected = get_field('display_where');
if( in_array('speight', $selected) ) {
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
endwhile;
wp_reset_postdata();
endif;
I’m wondering if the issue is that the ACF field isn’t somehow specified before the arg the same way it’s called in the loop itself? I mean, if all I’m telling it is “display_where” (the name of the field within the post type), without doing the whole get_field(‘display_where’) part of it, does it know how to see it? but I’ve tried calling it that way and it doesn’t make a difference.
The first problem with your code is that you specified the pagination for the main query, not the custom query ($query = new WP_Query( $args );
). This code will always show a different page number even if you have the right custom query.
Secondly, you manually hide the content by checking the custom field instead of filtering it from the custom query. This code will never show the right page number because your custom query doesn’t know that you hide certain contents after the query is initialized.
To learn how to show pagination with a custom query, please take a look at this page: http://callmenick.com/post/custom-wordpress-loop-with-pagination.
With the method described in that page, your first code is the closest one. Could you please share your JSON or XML export of your field group so I can check the query for you?
Thanks!
I did a print_r of the field group, if that helps?
array(1) {
[0]=>
string(7) "speight"
}
And that does make sense about the pagination. Thank you.
You can export your field group from “Custom Fields > Tools” menu on the left of your backend. Please upload it using the link icon on the left of the reply box.
Thanks 🙂
Thanks for the export file.
Could you please try the following arguments?
$args = array(
'post_type' => 'cpt',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
)
)
);
I also just noticed something wrong with your code. It should be something like this:
<?php $query = new WP_Query( $args );
if ($query->have_posts()) :
while ( $query->have_posts() ) : $query->the_post();
This page should give you more idea about it: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.
Hope this helps!
The topic ‘Trouble filter query on acf checkbox value and $paged’ 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.