I am creating a search function for a custom post type on my wordpress site, and I need to filter out search results based off an ACF True/False field. I have to use WP_Query to pass arguments since the generic wordpress loop does not allow that, but when I use WP_Query the query returns all the posts based on the arguments I passed, and disregards the actual search term.
<?php $args = array(
'post_type' => 'work',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'work_hidden',
'value' => '0',
'compare' => '=='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) {
$the_query->the_post();
include "partials/work-card.php";
}
endif; ?>
How can I use WP_Query to include the search term and the arguments.
Thanks so much!
First the 'compare' when you want equals in SQL MUST be a single '=';
Next you have to put the search parameter as from the Documentation, assuming is $_GET['search']
:
$args = array(
'post_type' => 'work',
'posts_per_page' => -1,
's' => $_GET['search'],
'meta_query' => array(
array(
'key' => 'work_hidden',
'value' => '0',
'compare' => '='
)
)
);