Support

Account

Home Forums Front-end Issues Add ACF Fields to WordPress Search

Solving

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 );
         ?>
  • 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
            );
    
  • how can i get the url?

  • what url are you trying to get?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.