I was using this before and it was working , but now the issue raised in it. I want to pick attached audios of a post. it is fetching old audios attached and not showing new attachments,
below is the function select_audios I call this and it is returing audios.
function select_audios()
{
header("Content-Type: text/html");
$args = $_REQUEST;
$cat_id = isset($args["cat_id"]) ? $args["cat_id"] : "";
$page_number = isset($args["paged"]) ? $args["paged"] : "";
$page_size = isset($args["page_size"]) ? $args["page_size"] : "";
$orderby = isset($args["orderby"]) ? $args["orderby"] : "";
$order = isset($args["order"]) ? $args["order"] : "";
$categories = get_categories();
$args = [
"post_type" => "post",
"paged" => $page_number,
"posts_per_page" => $page_size,
"tax_query" => [
[
"taxonomy" => "category",
"field" => "term_id",
"terms" => $cat_id,
],
],
"orderby" => $orderby,
"meta_key" => "order",
"order" => $order,
];
$audios_list = process_audio_items($args);
wp_reset_postdata();
$out = json_encode($audios_list);
unset($audios_list);
die($out);
}
add_action("wp_ajax_nopriv_select_audios", "select_audios");
add_action("wp_ajax_select_audios", "select_audios");
The second function is called by parent function it fetches the attached audios based on the ID
function process_audio_items($args) {
$loop = new WP_Query($args);
$audios_list = [];
if ($loop->have_posts()):
while ($loop->have_posts()):
$loop->the_post();
$audio_item = (object) [];
$img_url = get_the_post_thumbnail_url();
$audio_item->{'audio_img_url'} = $img_url;
$site_dir = get_site_url(null, "", null);
if (empty($img_url)) {
$audio_item->{'audio_img_url'} =
$site_dir .
"/wp-content/uploads/2020/05/cropped-512x512-Google-Android-Chrome-8-1.png";
}
$audio = get_posts([
"post_parent" => get_the_ID(),
"post_type" => "attachment",
"post_mime_type" => "audio",
]);
$meta = get_post_meta(get_the_ID());
$audio_item->{'id'} = htmlspecialchars($audio[0]->ID);
$audio_item->{'title'} = htmlspecialchars(get_the_title());
$audio_item->{'ordernumber'} = $meta["order"][0];
$audio_item->{'audio_post'} = get_home_url();
$audio_item->{'description'} = htmlspecialchars(get_the_excerpt());
$audio_item->{'audio_src'} = wp_get_attachment_url(
$audio[0]->ID,
"full"
);
array_push($audios_list, $audio_item);
unset($audio_item);
endwhile;
endif;
return $audios_list;
}