Support

Account

Home Forums ACF PRO Image field returns a string instead of an array

Solving

Image field returns a string instead of an array

  • Hi there, I’m facing an issue using ACF PRO and “WP Post Distance Filter”… I’ll try to explain myself as clearly as possible, but my english can be approximative sometimes ahah.

    I use a custom field called ‘event_cover’, which returns an image object. I display it in the loop using:

    <?php
    $image = get_field('event_cover');
            if( !empty($image) ): 
    ?>
    <img src="<?php echo $image['sizes']['medium'] ?>" alt="<?php echo $image['alt']; ?>" />
    <?php endif; ?>

    It works well on all pages, singles, archives, etc…

    I need the user to be able to filter the posts by distance from a choosen location.
    To determinate the distance between search location and the post location, I extract the datas of a google map custom field, and save the address and coordinates in 3 differents post-meta, using acf/save_post and update_post_meta.

    It seems to work too, as my post are renderd in the correct order in search results page. I also can display the distance from the search location in the post loop, the value rendered is the right one.

    BUT…

    In the search results page, I get this error :

    
    <br /> <b>Warning</b>:  Illegal string offset 'alt' in <b>...\wp-content\themes\wattzapplication\loop.php</b> on line <b>29</b><br /> 2

    <br /> <b>Warning</b>: Illegal string offset 'sizes' in <b>...\wp-content\themes\wattzapplication\loop.php</b> on line <b>29</b><br /> <br /> <b>Warning</b>: Illegal string offset 'cards' in <b>...\wp-content\themes\wattzapplication\loop.php</b> on line <b>29</b><br /> 2

    I tried to get the datas using : <pre><?php var_dump( $image ); ?> </pre> to test if the returned values were correct… and it appears that instead of an array, $image returns a string with the image ID: string(3) "302"

    I can’t figure out why the fu** the loop works everywhere… but not there!!!

    If someone there has some time to loose, a bit of help would be gladly appreciated.

    thanks πŸ˜‰

  • What code is used to get the image field where you get the string value?

    ACF stores the ID of the image in the database, not the image data. Only the ACF field settings allow ACF to return an image object. If you are using get_post_meta() then you’ll only get the image. I’m guessing that you are supplying a field name to the WP Post Distance Filter?

    Will need more information about the code that gets and displays the value.

  • hi John, thanks for answering.
    I have a template part named ‘loop.php’, which I use to display all loops in my application.

    In this file I use the following code to display image from custom field:

    <?php
    $image = get_field('event_cover');
            if( !empty($image) ): 
    ?>
    <img src="<?php echo $image['sizes']['medium'] ?>" alt="<?php echo $image['alt']; ?>" />
    <?php endif; ?>

    $image gets an array of values (as expected) in all pages, but not in search results page if posts are filtered by distance… it gets a string value instead.

    Not sure if I’m clear… sorry

  • I know these may sound like an obvious questions, but the code you have for displaying the images appears that it should be working.

    Is code is in “The Loop” of the search results page?

    Check to be sure that there is actually an attachment post with the post ID of 302 and that it is an image. This could cause the ID to be returned instead of the image array.

    Also, rather then if( !empty($image) ): you could do if (is_array($image)): This would be a better test.

  • My search page code is :

    <div class="row">
    <?php
    $loop = new WP_Query( array( 
    'post_type' => 'events','posts_per_page' => -1, 'order=ASC' )
    );
                if ($loop->have_posts() ):
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    			get_template_part( 'loop');
    
    				endwhile;
    
    			else :
    
    				get_template_part( 'loop', 'empty' );
    
    			endif;?>
    </div>

    and loop.php content code is (for example) :

    <article id="post-<?php the_ID(); ?>">
      <div class="card">
        <div class="card-image">
        <?php 
            $image = get_field('event_cover');
            if( !empty($image) ): ?>
    
            <img src="<?php echo $image['sizes']['medium'] ?>"/>
    
            <?php endif; ?>
        </div>
      </div>
    </article>

    This exact same snippet works for displaying the image of all posts in all pages…but not on search page πŸ™

    And 302 is the good ID (I mean, the ID of the image that should be displayed…).

    Thanks for your time and help John πŸ˜‰

    EDIT: some more details that may help.

    1) I have a taxonomy field named “event_style”. When I filter posts by this custom field it works great (The image is displayed correctly). It fails only when I use the distance filter to modify the query…

    2) I have another image field displayed (as an object) in the header (so it’s outside the loop). This one doesn’t work neither when posts are filtered by distance. Works well on other pages.

  • Anybody with a little chunck of idea, please?

  • I’ve been able to isolate the snippet that cause the issue. The problem comes from the custom query.

    Here is the code:

    /**
     * Filters posts by distance if URL parameters are set
     *
     * @param $query
     * @return void
     */
    function wpdf_filter( $query )
    {
    	if ( Wpdf::validate_url_params() && Wpdf::is_enabled() ) {
    		global $wpdf_url_loc;
    		$loc = $wpdf_url_loc;
    		if ( $loc['lat'] != '' && $loc['lng'] != '' ) {
    			global $wpdf_orderString;
    			add_filter( 'posts_where', 'wpdf_filter_where' );
    			add_filter( 'posts_orderby', 'wpdf_filter_orderby' );
    			unset( $wpdf_orderString );
    		}
    	}
    }
    add_action( 'pre_get_posts', 'wpdf_filter' );
    
    /**
     * Filter for where
     *
     * @param $query
     * @return string
     */
    function wpdf_filter_where( $where )
    {
    	global $wpdb, $wpdf_orderString;
    	$validPosts = array();
    	$orderStrings = array();
    	$queryStrings = array();
    	global $wpdf_url_loc;
    	$loc = $wpdf_url_loc;
    	$posts = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'lat' OR meta_key = 'lng' ORDER BY post_id" );
    	for ( $i = 0; $i < count( $posts ); $i += 2 ) {
    		$dis = Wpdf::distance( $posts[$i]->meta_value, $posts[$i + 1]->meta_value, $loc['lat'], $loc['lng'] );
    		$rad = Wpdf::get_rad();
    		if ( ( isset($_GET[$rad] ) && $dis < $_GET[$rad] ) || ! isset( $_GET[$rad] ) ) {
    			array_push( $queryStrings, 'wp_posts.ID = '.$posts[$i]->post_id );
    			array_push( $validPosts, array( 'distance' => number_format( $dis, 2 ), 'ID' => $posts[$i]->post_id ) );
    		}
    	}
    	usort( $validPosts, 'wpdf_compare' );
    	foreach ( $validPosts as $index => $validPost ) {
    		if ( intval( $index ) == count( $validPosts ) - 1 ) {
    			array_push( $orderStrings, 'ELSE '.$index );
    		} else {
    			array_push( $orderStrings, "WHEN '".$validPost['ID']."' THEN ".$index );
    		}
    	}
    	$queryString = ' AND (' . implode( ' OR ', $queryStrings ) . ')';
    	if ( count( $validPosts ) > 1 ) {
    		$wpdf_orderString = ' CASE wp_posts.ID ' . implode( ' ', $orderStrings ) . ' END';
    	} else {
    		$wpdf_orderString = '';
    	}
    	return $where . $queryString;
    }
    
    /**
     * Filter for orderby
     *
     * @param $query
     * @return string
     */
    function wpdf_filter_orderby( $orderby )
    {
    	global $wpdb, $wpdf_orderString;
    	return $wpdf_orderString;
    }
    
    /**
     * Associate array comparison
     *
     * @param query
     * @return positive/negative
     */
    function wpdf_compare( $a, $b )
    {
    	return $a['distance'] == $b['distance'] ? 0 : ($a['distance'] < $b['distance']) ? -1 : 1;
    }

    There is obviously something in this part of code that cause the problem, but I can’t figure what…

    PS: I’m aware that my problem is not directly related to ACF in itself, but I don’t know where to ask for help. thanks in advance πŸ˜‰

  • Sorry for the delayed response on this, I’ve been having an issue not getting notifications of new comments.

    Can you disable (comment out) these filters and only have one at a time working, or remove them all. It would probably help if you could narrow this down to the filter function that’s responsible.

  • Hi John,

    it’s an old thread but i have the same problem what causes for me the problem is the filter: pre_get_posts

    pre_get_posts function conflict with ACF Field

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

The topic ‘Image field returns a string instead of an array’ is closed to new replies.