Reputation: 587
whenever i try to use get_the_ID() inside get_attached_file( ) i always get blank response example get_attached_file( get_the_ID() )
but if i use normal number like get_attached_file( 4125 )
its totaly work
what should i do for fix it ?
my code now :
switch ($command) {
case 'list_product':
$loop = new WP_Query(
array(
'post_type' => 'product'
)
);
if( $loop->have_posts() ) :
$data = array( "api_status" => 1, "api_message" => "success");
// First we get the image id
// $post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
// // Then we get the image data, we will get an array with some informations
// $image = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
// // the image url is the first index of this array
// $image_url = $image[0];
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
$meta[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"stock_status" => get_post_meta( get_the_ID(), '_stock_status', true ),
"price" => get_post_meta( get_the_ID(), '_price', true ),
"reguler_price" => get_post_meta( get_the_ID(), '_regular_price', true ),
// "image" => basename( get_attached_file( $post_id ) ),
);
endwhile;
endif;
echo json_encode($meta);
break;
Upvotes: 1
Views: 407
Reputation: 68
According to this article see if this works:
$loop = new WP_Query(
array(
'post_type' => 'product'
)
);
if( $loop->have_posts() ) :
$data = array( "api_status" => 1, "api_message" => "success");
// First we get the image id
// $post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
// // Then we get the image data, we will get an array with some informations
// $image = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
// // the image url is the first index of this array
// $image_url = $image[0];
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
//get the first attachment. Not sure if this is the one you want
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
$attachment_ID = $attachments[0]->ID;
$meta[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"stock_status" => get_post_meta( get_the_ID(), '_stock_status', true ),
"price" => get_post_meta( get_the_ID(), '_price', true ),
"reguler_price" => get_post_meta( get_the_ID(), '_regular_price', true ),
"image" => basename( get_attached_file( $attachment_ID ) ),
);
endwhile;
Upvotes: 2
Reputation: 43
Declare a $post
variable global and use $post->ID
instead of $ID
or get_the_id
, Hope this help
Upvotes: 0
Reputation: 51
You can try to assign the ID to a variable first -
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
$ID = get_the_ID()
$meta[] = array(
"id" => $ID,
"post_name" => get_the_title(),
"stock_status" => get_post_meta( $ID, '_stock_status', true ),
"price" => get_post_meta( $ID, '_price', true ),
"reguler_price" => get_post_meta( $ID, '_regular_price', true ),
"image" => basename( get_attached_file( $ID ) ),
);
endwhile;
Upvotes: 1