phparrayswordpress

Pass array value in query post in WordPress


My WordPress code below code produces:

Array ( [0] => 393 [1] => 362 [2] => 361 )

I want to convert this array into array(393,362,361) and I want to pass this array into WordPress's query posts.

$getphotoidfromurl = 5;
global $wpdb;
// $sqlselque = "SELECT post_id FROM wp_postmeta WHERE meta_key = 'metrodir_company_gallery' LIMIT 0 , 30";
    
$db_item = $wpdb->get_results($wpdb->prepare("SELECT post_id,meta_value FROM wp_postmeta WHERE meta_key = 'metrodir_company_gallery' LIMIT 0 , 30"));
$r1 = array();
foreach ($db_item as $rs) {
    // echo $rs->post_id;
    $getphotos = $rs->meta_value;
    $rr = explode(",", $getphotos);
    $getphotocount = count($rr);
    
    if ($getphotoidfromurl == $getphotocount) {
        $newgettwo = $rs->post_id;
        // print_r($array);
        array_push($r1, $newgettwo);
    }
}
print_r($r1);
exit;
global $wp_query;
    
query_posts(array(
    'post_type' => array('company'),
    'posts_per_page' => -1,
    'orderby' => $orderby,
    'order' => $order,
));

My WordPress query should look like this:

query_posts(array(
    'post_type' => array('company'),
    'posts_per_page' => -1,
    'orderby' => $orderby,
    'order' => $order,
    'p'  => array(393,362,361)
));

Solution

  • Just simply put $r1, because your resulted $r1 is fine,when we print_r($r1); it show us array key index

             query_posts(array(
                        'post_type' => array('company'),
                        'posts_per_page' => -1,
                        'orderby' => $orderby,
                        'order' => $order,
                        'p'  => $r1  //see here 
    

    e.g

    $r1=array(393,362,361);
    print_r($r1);
        //result        
         Array ( [0] => 393 [1] => 362 [2] => 361 )