admin管理员组文章数量:1393375
I try to get_posts like this:
$sql = $wpdb->get_results( $querystrShop );
// Create array of product ID
foreach ($sql as $prd)
{
$arraycip = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in'=> array($prd->post_id),
'fields' => 'ids'
);
}
$sameCIParrayOfID = get_posts($arraycip);
But they are only first post:
$sql return 2 products $arraycip have only the first product
Thanks,
EDIT 1
tried another think:
$html = urldecode( $_GET['product_search'] );
//printf($html);
// Get the category ID from url
$catsearched = $_GET['product_cat'];
//printf($catsearched);
$earth_radius = 6371;
// Get products of category with in range distance
$querystrShop = $wpdb->prepare("
SELECT productid
AS post_id,(%d * ACOS(COS(RADIANS(%s)) * COS(RADIANS(phiz_geo_location.latitude)) * COS(RADIANS(phiz_geo_location.longitude) - RADIANS(%s)) + SIN(RADIANS(%s)) * SIN(RADIANS(phiz_geo_location.latitude))))
AS distance, phiz_postmeta.meta_value
FROM phiz_geo_location
JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id
LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id
LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id
WHERE phiz_term_relationships.term_taxonomy_id IN ($catsearched)
AND p.post_title ='".$html."'
AND p.post_status = 'publish'
HAVING distance < %f",
$earth_radius,
$latitude,
$longitude,
$latitude,
$radius
);
$sql = $wpdb->get_results( $querystrShop );
$productArrayShop = array();
foreach ($sql as $prd)
{
$productArrayShop[] = $prd->post_id;
}
$res = implode(",", $productArrayShop);
$arg = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in'=> array($res),
'fields' => 'ids'
);
$sameCIParrayOfID = get_posts($arg);
Only first product for variable $sameCIParrayOfID but the variable $res return 2 products
string(15) "2146031,2176394" array(1) { [0]=> int(2146031) }
I try to get_posts like this:
$sql = $wpdb->get_results( $querystrShop );
// Create array of product ID
foreach ($sql as $prd)
{
$arraycip = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in'=> array($prd->post_id),
'fields' => 'ids'
);
}
$sameCIParrayOfID = get_posts($arraycip);
But they are only first post:
$sql return 2 products $arraycip have only the first product
Thanks,
EDIT 1
tried another think:
$html = urldecode( $_GET['product_search'] );
//printf($html);
// Get the category ID from url
$catsearched = $_GET['product_cat'];
//printf($catsearched);
$earth_radius = 6371;
// Get products of category with in range distance
$querystrShop = $wpdb->prepare("
SELECT productid
AS post_id,(%d * ACOS(COS(RADIANS(%s)) * COS(RADIANS(phiz_geo_location.latitude)) * COS(RADIANS(phiz_geo_location.longitude) - RADIANS(%s)) + SIN(RADIANS(%s)) * SIN(RADIANS(phiz_geo_location.latitude))))
AS distance, phiz_postmeta.meta_value
FROM phiz_geo_location
JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id
LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id
LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id
WHERE phiz_term_relationships.term_taxonomy_id IN ($catsearched)
AND p.post_title ='".$html."'
AND p.post_status = 'publish'
HAVING distance < %f",
$earth_radius,
$latitude,
$longitude,
$latitude,
$radius
);
$sql = $wpdb->get_results( $querystrShop );
$productArrayShop = array();
foreach ($sql as $prd)
{
$productArrayShop[] = $prd->post_id;
}
$res = implode(",", $productArrayShop);
$arg = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in'=> array($res),
'fields' => 'ids'
);
$sameCIParrayOfID = get_posts($arg);
Only first product for variable $sameCIParrayOfID but the variable $res return 2 products
string(15) "2146031,2176394" array(1) { [0]=> int(2146031) }
Share
Improve this question
edited Mar 20, 2020 at 22:39
ilanb
asked Mar 20, 2020 at 21:35
ilanbilanb
933 silver badges12 bronze badges
3
|
1 Answer
Reset to default 0// Get the category ID from url
$catsearched = $_GET['product_cat'];
//printf($catsearched);
$earth_radius = 6371;
// Get products of category with in range distance
$querystrShop = $wpdb->prepare("
SELECT productid
AS post_id,(%d * ACOS(COS(RADIANS(%s)) * COS(RADIANS(phiz_geo_location.latitude)) * COS(RADIANS(phiz_geo_location.longitude) - RADIANS(%s)) + SIN(RADIANS(%s)) * SIN(RADIANS(phiz_geo_location.latitude))))
AS distance, phiz_postmeta.meta_value
FROM phiz_geo_location
JOIN phiz_term_relationships ON phiz_geo_location.productid = phiz_term_relationships.object_id
LEFT JOIN phiz_postmeta ON phiz_geo_location.productid = phiz_postmeta.post_id
LEFT JOIN phiz_posts AS p ON phiz_postmeta.post_id = p.id
WHERE phiz_term_relationships.term_taxonomy_id IN ($catsearched)
AND p.post_title ='".$html."'
AND p.post_status = 'publish'
GROUP BY post_id
HAVING distance < %f",
$earth_radius,
$latitude,
$longitude,
$latitude,
$radius
);
$sql = $wpdb->get_results( $querystrShop );
$sameCIParrayOfID = wp_list_pluck( $sql, 'post_id' );
本文标签: wp querygetposts return only first result
版权声明:本文标题:wp query - get_posts return only first result 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652044a2617742.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'post__in'=> $productArrayShop,
– Michael Commented Mar 21, 2020 at 1:34$productArrayShop = wp_list_pluck( $sql, 'post_id' );
without having to do theforeach
. ;) And remember that an emptypost__in
would include all posts (that match the other args). So you may want to check if the array ($productArrayShop
) is not empty. – Sally CJ Commented Mar 21, 2020 at 2:49