Home › Forums › ACF PRO › Image field returns a string instead of an array › Reply To: Image field returns a string instead of an array
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 😉
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.