admin管理员组文章数量:1130349
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();
}
}
}
}
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();
}
}
}
}
本文标签: How to query for exact string in custom field
版权声明:本文标题:How to query for exact string in custom field? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749150402a2323718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论