Home › Forums › Front-end Issues › Add ACF Fields to WordPress Search
Is there a way to alter the functions.php file so it allows people to search based on an image title, caption, alt text or description, and receive the related result (image) from the ACF Gallery?
Example: I have a hardwood company website, if the user searches for ‘Red Oak’, return all product images that contain the image title ‘Red Oak’.
Thanks in advance!
Below is the code i have that’s just searching through my custom post type “products”. Within each custom post type is an ACF gallery of images.
<?php
$s = get_search_query();
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1,
's' => $s
);
$the_query = new WP_Query( $args );
?>
Hi @joshuaisaac
This plugin maybe able to help in this https://wordpress.org/plugins/acf-better-search/
Thank’s for the reply James. I tried this plugin but it still returns nothing if i search by image title, alt text, or description of image from the gallery.
Your problem is that you are searching the post type of products. The information about each image is not associated with the product, this information is part of the post for the image which is an “attachment” post type. If you want to search images then you need to search attachments.
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'ASC',
'posts_per_page' => -1,
's' => $s
);
The topic ‘Add ACF Fields to WordPress Search’ 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.