start_controls_section( 'section_content', [ 'label' => 'Settings', ] ); $this->end_controls_section(); } protected function render(){ $settings = $this->get_settings_for_display(); $location = get_user_location(); $current_country = $location['country_code']; // default settings $post_filter = 'recent'; $posts_per_page = 6; $page = 1; if (!empty($_GET['post_filter'])) { $post_filter = $_GET['post_filter']; } if (!empty($_GET['page_no'])) { $page = $_GET['page_no']; } $args = [ 'posts_per_page' => $posts_per_page, 'post_type' => 'post', 'post_status' => array('publish'), 'paged' => $page, ]; if ($post_filter == 'recent') { $args = array_merge($args, [ 'ignore_sticky_posts' => 1, 'orderby' => 'post_date', 'order' => 'DESC', ]); } else if ($post_filter == 'newest') { $args = array_merge($args, [ 'ignore_sticky_posts' => 0, 'orderby' => 'post_date', 'order' => 'DESC', ]); } else if ($post_filter == 'trending') { $args = array_merge($args, [ 'ignore_sticky_posts' => 1, 'orderby' => 'ID', 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'is_trending', 'value' => '1', 'compare' => '==' ) ) ]); } else if ($post_filter == 'hottest_stories') { $args = array_merge($args, [ 'ignore_sticky_posts' => 1, 'meta_key' => 'float_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ]); } else if ($post_filter == 'oldest') { $args = array_merge($args, [ 'ignore_sticky_posts' => 1, 'order' => 'ASC' ]); } // Insert query by country if (isset($args['meta_query'])) { $metaQuery = $args['meta_query']; $metaQuery = array_merge($metaQuery, [ 'relation' => 'AND', array( 'relation' => 'OR', array( 'key' => 'location', 'value' => $current_country, 'compare' => 'LIKE', ), array( 'key'=> 'location', 'compare' => 'NOT EXISTS' ), array( 'key'=> 'location', 'value' => '', 'compare' => '=' ) ) ]); $args['meta_query'] = $metaQuery; } else { $args = array_merge($args, [ 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'location', 'value' => $current_country, 'compare' => 'LIKE', ), array( 'key'=> 'location', 'compare' => 'NOT EXISTS' ), array( 'key'=> 'location', 'value' => '', 'compare' => '=' ) ) ]); } $count_query_args = $args; $count_query_args['posts_per_page'] = -1; $post_query = new WP_Query( $args ); $count_query = new WP_Query($count_query_args); $posts = $post_query->posts; $count = $count_query->post_count; if ($count === 0 || $count < $posts_per_page) { $total_page = 0; } else { $total_page = intval($count) / intval($posts_per_page); $total_page = $total_page / intval($total_page) > 0 ? intval($total_page) + 1 : intval($total_page); } $filterMap = [ 'recent' => 'Recent', 'trending' => 'Trending', 'hottest_stories' => 'Hottest Stories', 'newest' => 'Newest', 'oldest' => 'Oldest' ]; // Filter block $html = '