$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to query for exact string in custom field?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to query for exact string in custom field?

matteradmin7PV0评论

I have a custom field which I set in a var

$customCoords = usp_get_meta(false, 'usp-custom-90');

That custom field has a string like this:

45.46135436613811,9.175124650000043

Then I get some values from a form:

$lat = $_GET['usp-custom-19'];
$ln = $_GET['usp-custom-20'];

And I join them like this:

$coordinates = $ln . "," .$lat;

Now $coordinates is a string like this:

45.46135436613811,9.175124650000043

And now I want to query all posts which have the same exact string as $coornidates and I do

$args = get_posts( 
    array( 
        'post_type'      => 'post', 
        'posts_per_page' => -1, 
        'meta_query'     => array(
            array(
                'key'     => $customCoords,
                'value'   => $coordinates,
                'compare' => '=',
                'type' => 'CHAR'
            )
        ) 
    ) 
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo the_title(); 
        echo "<br>";
    }
}

But I get nothing.

UPDATE

$lat = $_GET['usp-custom-19'];
$ln = $_GET['usp-custom-20'];

$args = get_posts( 
    array( 
        'post_type'      => 'post', 
        'posts_per_page' => -1, 
    ) 
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $customCoords = usp_get_meta(false, 'usp-custom-90');
        $arrayCoords = explode( ",", $customCoords );
        $radiusLn = +$arrayCoords[0] + 10;
        $radiusLat = +$arrayCoords[1] + 10;
        $args = array(
            'post_type' => 'post',
            'meta_query' => array(
                array(
                    'relation' => 'AND',
                    array(
                        'key' => 'get_usp-custom-19',
                        'value' => array($ln, $radiusLn),
                        'compare' => '>='
                    ),
                    array(
                        'key' => 'get_usp-custom-20',
                        'value' => array($lat, $radiusLat),
                        'compare' => '<='
                    ),
                ),
            ),
        );
        $query = new WP_Query( $args );
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                the_title();
            }
        }
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far