Support

Account

Forum Replies Created

  • Hi deset,
    stuck with the same issue here… did you find a trick to avoid this problem?

  • 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 😉

  • Anybody with a little chunck of idea, please?

  • 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.

  • 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

  • Hi Chris and thanks for this very helpfull plugin!!! I’m facing an issue with the filtering of posts by custom fields value, in angular. Maybe you can help me?
    I thought I could easily do it using:

    ../wp-json/posts/?type=some_custom_type&filter[X]=Y

    But I cant get it to work. Do you know a way to do it?
    Thanks again for you help

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