Forums › Forums › Search & Filter Pro › Search not working with ACF fields in wp_query
- This topic has 10 replies, 2 voices, and was last updated 5 years, 9 months ago by Anonymous.
-
Anonymous(Private) January 28, 2019 at 2:55 pm #200152
I want to get search results based on the pages ACF fields which set the post_type and search_filter_id
It works on page load but then when changing the search criteria, it seems to just load all uncategorized posts and forgets the fields for post type and id. Here is my code:
<?php echo do_shortcode('[searchandfilter id="'.get_field('search_id').'"]'); ?> <div class="buy-wrapper" id="search-results"> <?php //$postType = get_field('post_type'); //echo $postType; $args = array('post_type' => get_field('post_type')); $args['search_filter_id'] = get_field('search_id'); $category_posts = new WP_Query($args);
It’s worth noting this works fine, when hard coded post type and IDs are used. Is there a work around for this?
Trevor(Private) January 28, 2019 at 3:09 pm #200155Is it just these two lines that do not work?
$args = array('post_type' => get_field('post_type')); $args['search_filter_id'] = get_field('search_id');
If you use an echo with a message to prefix those lines, do the post type and id output or are they empty?
Does the form in the first line of the code output OK?
What happens if you add the Post ID as the second argument. E.g.
$args = array('post_type' => get_field('post_type', get_the_ID())); $args['search_filter_id'] = get_field('search_id', get_the_ID());
Anonymous(Private) January 28, 2019 at 3:20 pm #200161I have made the code a little cleaner, I tried get_the_ID() and this did not work, I also have taken the variables out of the AJAX container (#search-results) and still get the same issue.
The form in the first line does work fine, it is literally just the results that go to all uncategorised. Echoing those 2 args, produces the correct array and ID.
<?php $postType = get_field('post_type', get_the_ID()); $searchId = get_field('search_id', get_the_ID()); echo do_shortcode('[searchandfilter id="'.$searchId.'"]'); $args = array('post_type' => $postType); $args['search_filter_id'] = $searchId; print_r($args = array('post_type' => $postType)); echo $args['search_filter_id'] = $searchId; ?> <div class="buy-wrapper" id="search-results"> <?php $category_posts = new WP_Query($args); if($category_posts->have_posts()) : while($category_posts->have_posts()) : $category_posts->the_post(); ?>
Anonymous(Private) January 28, 2019 at 3:43 pm #200172The data is blank after the search, you are right. Thank-you for your help so far, what do you suggest?
Array
(
[post_type] =>
[search_filter_id] =>
);
<?php $postType = get_field('post_type', get_the_ID()); $searchId = get_field('search_id', get_the_ID()); echo do_shortcode('[searchandfilter id="'.$searchId.'"]'); ?> <div class="buy-wrapper" id="search-results"> <?php $args = array('post_type' => $postType); $args['search_filter_id'] = $searchId; ?> <pre><?php print_r($args);?></pre>; <?php $category_posts = new WP_Query($args); if($category_posts->have_posts()) : while($category_posts->have_posts()) : $category_posts->the_post(); ?>
Trevor(Private) January 28, 2019 at 5:26 pm #200244From your original note, I got that you would be using more than one real page, yes? The problem is that the ‘As an archive’ method is designed NOT to be used with existing pages, but pages that WordPress creates ‘on the fly’ and which do not have Post IDs and therefore cannot have custom fields.
The only Display method suitable for use as you intend is the ‘Custom’ method, but that requires one form per page (as the results URL is fixed). I have used this method many times in the past, with an ACF field on the page to store the form ID and other stuff. Then the do_shortcode calls that variable to complete the shortcode).
-
AuthorPosts