WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Meta Query Location Distance (5 posts)

  1. Guiant
    Member
    Posted 1 year ago #

    Hello,

    How to do something with the data returned by a key in a meta query ?

    I would like to transform this :

    <?php
    if ((6371 * acos( cos( deg2rad($latsearch) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($lngsearch) ) + sin( deg2rad($latsearch) ) * sin( deg2rad( $lat ) ) ) ) < 25) {
    }
    ?>

    into a query

    with something like this :

    <?php
    array(
    	 'key' => (6371 * acos( cos( deg2rad($latsearch) ) * cos( deg2rad( KEY[0] ) ) * cos( deg2rad( KEY[1] ) - deg2rad($lngsearch) ) + sin( deg2rad($latsearch) ) * sin( deg2rad( KEY[0] ) ) ) ),
    	'value' => 25,
    	'compare' => '<='
    )
    ?>

    For a meta_query.

    is it possible ?

    Thanks a lot.
    Guillaume.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Do you get your variables from a search form? Or are these custom field values ($latsearch, $lngsearch, etc)?

  3. Guiant
    Member
    Posted 1 year ago #

    Thanks for this very quick answer.

    $latsearch & lngsearch are from a search form and KEY[0] and KEY[1] are from the database.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Meta-data is handled with key/value pairs. So if you give your posts a custom field "location" with a numeric value you can query posts for that value like this example:

    <?php
    if ((6371 * acos( cos( deg2rad($latsearch) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($lngsearch) ) + sin( deg2rad($latsearch) ) * sin( deg2rad( $lat ) ) ) ) < 25) :
    $args = array(
    	'meta_query' => array(
    		array(
    			'key' => 'location',
    			'value' => 25,
    			'compare' => '<=',
    			'type' => 'numeric',
    		)
    	)
    );
    query_posts( $args );
    ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <!-- loop for location smaller than 25 -->
    <?php endwhile; ?>
    <?php else : ?>
    <!-- do stuff for other location values -->
    <?php endif; ?>
  5. Guiant
    Member
    Posted 1 year ago #

    Thanks, I will try something like this.

    Guillaume.

Topic Closed

This topic has been closed to new replies.

About this Topic