Support

Account

Home Forums ACF PRO ACF Image URL not working in search results

Solved

ACF Image URL not working in search results

  • Greetings

    I have an image upload field on a CPT and have selected the “image URL” for saving, that’s what I want to use in my templates, not an array.

    This CPT is then related to another CPT via the Relationship field.

    I can display image the from the first CPT just fine in normal WP pages for the second CPT using the ‘get_field’ method, with the following code:

    $relatedposts = get_field('applicable_resort'); //This is the related CPT
    if( $relatedposts ) { 
      foreach( $relatedposts as $relatedpost):  setup_postdata($relatedpost);
        $logo = get_field('logo'); // This is the image
    

    Then I just use

    <img src="<?php echo $logo ?>
    

    Where I want the image to display. Works great.

    The problem I’m having is that I use Search & Filter Pro for search results, and in the template that displays the results that method and code does not return the URL.

    I think the issue is that on a normal page, I’m dealing with one post and it’s easy to get the associated related metadate, but on a search results page, I’m dealing with a query that returns a lot of posts….the code doesn’t cause errors, but the URL for the image is blank, so it shows the ‘broken image’ placeholder.

    I’ve tried using instead this:
    $logo = get_post_meta($relatedpost->ID,'logo',true);

    But that doesn’t work either. When I look in my DB, the wp_postmeta table, there is nothing at all stored in the ‘logo’ field, but in the ‘_logo’ field there is a value that I dont’ recognize as a URL – it looks like this: field_5717de1240fbb

    I’m guessing it’s the WP attachment ID, but I don’t know how to use that to echo the URL or display the image.

    Also, I tried using a var dump like this:

    
    $image = get_field('logo');
    
    echo '<pre>';
    	var_dump( $image );
    echo '</pre>';
    

    But it just displayed ‘NULL’.

    Any help would be most appreciated!

  • The problem is getting the related post data, you need to supply the post id from the loop running on the search results page

    
    $relatedposts = get_field('applicable_resort', $post->ID); //This is the related CPT
    
  • Hi John – thank you so much for the quick reply – I accidentally clicked on “this solved my problem” when I was trying to click on the slider to read to the right (the page jumped on me at the last second!)….

    Sadly this did not actually work and I don’t know mark this as “unsolved” still……

    I tried both the method you have (get_field(‘applicable_resort’, $post-ID)); as well as the standard get_post_meta, neither worked. Still no image URL.

    I’ve been looking at wp_get_attachment_image function but not sure how to use it.

    How can I mark this as unsresolved?

  • I don’t think you can unselect a solution.

    I would need to see the code from the loop in the search results page to be able to know more. Can you post the code from the loop that you’re trying to modify?

  • Thanks John, I appreciate your continued help.

    I’m actually not modifying the Loop per se, it’s a search result page so the ‘loop’ is standard, and on the template it starts out with the usual (if(have_posts()) : while (have_posts()) : the_post();)

    Then below that I have the code that sets up all the custom field metadata, some from the current post and some from the ‘related’ (via a relationship field) post – this code is:

    global $post;     
    $bookingstart = get_field('booking_date_start');
    $bookingend = get_field('booking_date_end');
    $travelstart = get_field('travel_date_start');
    $travelend = get_field('travel_date_end');
    $promocode = get_field('booking_promo_code');
    
    $relatedposts = get_field('applicable_resort',$post->ID);
    if( $relatedposts ) { 
      foreach( $relatedposts as $relatedpost):  setup_postdata($relatedpost);
        $resortname = get_the_title($relatedpost->ID);
        $location = get_post_meta($relatedpost->ID,'location',true);
        $logo =  get_field('logo'); 
        $demoterms = get_the_terms($relatedpost->ID, 'demographic_category');
          if ( !is_wp_error($demoterms)) {
           $demographic_category_nolinks = wp_list_pluck($demoterms, 'name'); 
           $demographic_category_list = implode(", ", $demographic_category_nolinks);
          }
      endforeach;
      }

    The reasoning behind all this is that we have two CPTs – “Deals” (‘deal’) and “Resorts” (‘resort’)……we started out with just Deals, but it quickly became apparent it was too cumbersome to keep entering what it resort-specific information into each deal, so we created the second CPT for Resorts to hold all that information that is specific to the resort, then we relate the Deal to the Resort.

    It’s all worked great everywhere except for on the search results pages, and only on this one field – the logo – that I can’t seem to get the URL for it to echo into the image tag so it can display – it works fine on other pages, just not on the search results page.

    What seems odd to me is that when I look at my wp_postmeta table in phpMyAdmin, I see the metakey (‘logo’) but there is no value….however in it’s corresponding _metakey (‘_logo’) there is a value but it’s not a URL, so I’m not sure how to use it – it is the word field followed by a number.

  • See my comments below

      
      // you don't need this, it's already done for this template
      //global $post; 
          
      $bookingstart = get_field('booking_date_start');
      $bookingend = get_field('booking_date_end');
      $travelstart = get_field('travel_date_start');
      $travelend = get_field('travel_date_end');
      $promocode = get_field('booking_promo_code');
      
      $relatedposts = get_field('applicable_resort',$post->ID);
      if( $relatedposts ) { 
        
        // change this line
        // then use $post instead of $relatedpost
        foreach( $relatedposts as $post):  
        
          setup_postdata($post);
          $resortname = get_the_title($post->ID);
          $location = get_post_meta($post->ID,'location',true);
          $logo =  get_field('logo'); 
          $demoterms = get_the_terms($post->ID, 'demographic_category');
            if ( !is_wp_error($demoterms)) {
             $demographic_category_nolinks = wp_list_pluck($demoterms, 'name'); 
             $demographic_category_list = implode(", ", $demographic_category_nolinks);
            }
        endforeach;
      }
      
      // important, reset search loop
      wp_reset_postdata();
    
  • Thanks John, I am actually laughing now, because that is how I started out…..and it made everything wonky…..I was getting the Deals post titles in place of the (applicable) Resort name, and lots of other things out of place. When I had wp_reset_postdata in place, everythiing after the first post was blank….oddly it showed the post div, with my HTML in place, but all the meta data was missing….

    BUT I will try it again, at this point I will try anything! AND it’s entirely possible that I may have originally had something tiny out of place – I just assumed because I was refererring to new posts, the global $post was confusing things and I had to change the second instance of $post to something else ($mypost, whatever, I just chose $relatedpost) ….maybe removing global $post will work, in the past I’ve needed it to access the custom metadata, which the standard WP loop doesn’t (or didn’t) make available.

  • EUREKA! John, you are a genius. I think if I had just removed the global $post in the very beginning it would have worked fine – when I put the code back to the way it was before (and exactly as you suggested) and removed global $post, it is working perfectly.

    Thank you a huge amount! If this forum had the emoji for a hug, I would include it here. πŸ™‚

  • The symptoms you describe are definitely a side effect of resetting post data incorrectly, it can cause some strange results. Glad you have it working.

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

You must be logged in to reply to this topic.