最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - get_posts return only first result

programmeradmin2浏览0评论

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 second code, use directly 'post__in'=> $productArrayShop, – Michael Commented Mar 21, 2020 at 1:34
  • 1 And you could also just do $productArrayShop = wp_list_pluck( $sql, 'post_id' ); without having to do the foreach. ;) And remember that an empty post__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
  • 1 Thanks @SallyCJ wp_list_pluck works fine ! – ilanb Commented Mar 21, 2020 at 10:55
Add a comment  | 

1 Answer 1

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' );
发布评论

评论列表(0)

  1. 暂无评论