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) }
3
  • 1
    second code, use directly 'post__in'=> $productArrayShop,
    – Michael
    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
    Mar 21, 2020 at 2:49
  • 1
    Thanks @SallyCJ wp_list_pluck works fine !
    – ilanb
    Mar 21, 2020 at 10:55

1 Answer 1

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' );
1
  • 1
    If you don't actually need to access the other columns (e.g. distance), you should select only the post_id column in your SQL ($querystrShop) - SELECT phiz_postmeta.post_id and then do $sameCIParrayOfID = $wpdb->get_col( $querystrShop ); .. I.e. Select only the column you need; it can improve the site's performance. :)
    – Sally CJ
    Mar 21, 2020 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.