and here's our server side code we have used to fetch the data
<?php
header('Access-Control-Allow-Origin: *');
?>
<?php
include_once "../wp-load.php";
function getTermsByID($ID, $taxonomy = 'category', $field = 'slug')
{
$terms = wp_get_post_terms($ID, $taxonomy, $args);
$termsArray = array();
if (!is_wp_error($terms)) {
foreach ($terms as $term) {
$termsArray[] = $term->$field;
}
}
return $termsArray;
}
$postData = array();
$currentPostID = $_GET['id'];
$posts_per_page = td_util::get_option('td_posts_per_page_in_mobile');
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page'=> $posts_per_page ? $posts_per_page : 20,
'order'=>'DESC',
'orderby'=>'date',
'tax_query'=>array(array('taxonomy'=>'category','field'=>'id','terms'=>array($_GET['cid'])))
);
$object = new WP_Query($args);
if ($object->have_posts()):
while ($object->have_posts()) {
$object->the_post();
$content = '';
//get post ids
$ID = get_the_ID();
//get categories
$categories = getTermsByID($ID, 'category', 'slug');
// get images
$fullfeatureimage = $thumbfeatureimage = '';
if (has_post_thumbnail($currentPost->ID)) {
$fullfeatureimage = wp_get_attachment_image_src(get_post_thumbnail_id($ID), 'full');
$thumbfeatureimage = wp_get_attachment_image_src(get_post_thumbnail_id($ID), 'thumbnail');
}
// get content
$content = preg_replace('#(\\r\\n|\\r|\\n)#', '<br>', get_the_content());
$pattern = '#https://twitter.com/[a-zA-Z_0-9]+/status/+[0-9]+#';
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $match) {
$urlSplit = explode('/',$match);
$replace = '[tweet id="'.end($urlSplit).'" width="100%"]<script async="" src="https://platform.twitter.com/widgets.js"></script>';
//replace data inside of those blocks
$content = str_replace($match,$replace,$content);
}
//https://www.youtube.com/watch?v=05CChwSIW14
$content = str_replace('https://www.youtube.com/watch?v=','https://youtu.be/',$content);
$content = str_replace('https://m.youtube.com/watch?v=','https://youtu.be/',$content);
$patternyoutube = '#https://youtu.be/[-a-zA-Z_0-9]+#';
preg_match_all($patternyoutube, $content, $matchesyoutube);
foreach ($matchesyoutube[0] as $match) {
$replaceyoutube = '<div class="youtubevideo">[youtube width="100%" height="300px"]'.trim($match).'[/youtube]</div>';
//replace data inside of those blocks
$content = str_replace($match,$replaceyoutube,$content);
}
$content = preg_replace("/<a\s(.+?)>(.+?)<\/a>/is", "$2", $content);
// add data to array
$currentPostDetails['id'] = $ID;
$currentPostDetails['url'] = get_permalink($ID);
$currentPostDetails['name'] = get_the_title();
$currentPostDetails['content'] = preg_replace('#<script>(.*?)</script>#', '', do_shortcode($content));
$currentPostDetails['date'] = date('H:i', strtotime(get_the_time())) . ' | ' . date('D d M Y', strtotime(get_the_date()));
$currentPostDetails['fullfeatureimage'] = $fullfeatureimage[0];
$currentPostDetails['thumbfeatureimage'] = $thumbfeatureimage[0];
$currentPostDetails['category'] = $categories ? implode(',', $categories) : '';
$currentPostDetails['urls'] = $matchesyoutube;
// get data in array
$postData[] = $currentPostDetails;
}
endif;
echo json_encode($postData);
die();
?>