Home / WP Advance Post query with meta taxonomy & pagination

WP Advance Post query with meta taxonomy & pagination

<?
    function GetPosts($data = null){
        $Return = new stdClass;

        if($data){
            extract($data);
        }
        $page = ($page)?:1;


        $PerPage = ($PerPage)?:($MangaScans['pagination_manga_listing'])?:8;
        if($page < 2){
            $Offset = 0;
        }else{
            $Offset = ($page - 1) * $PerPage;
        }

        $args['posts_per_page'] = $PerPage;
        $args['offset']         = $Offset;
        $args['post_type']      = 'manga';
        $args['post_status']    = 'publish';

        if($order_by == 'date_desc'){
            $args['orderby']        = 'date';
            $args['order']          = 'DESC';
        }elseif($order_by == 'date_asc'){
            $args['orderby'] = 'date';
            $args['order']   = 'ASC';
        }elseif($order_by == 'popular'){
            $args['meta_key'] = 'post_views_count';
            $args['orderby']  = 'meta_value_num';
            $args['order']    = 'DESC';
        }

        if($post_tag){
            $tax_query[] = [
                'taxonomy' => 'post_tag',
                'field'    => 'term_id',
                'terms'    => $post_tag,
                'operator' => 'IN',
            ];
        }
        
        if($post_tag){
            $meta_query[] = array(
                'key'     => 'meta_key',
                'value'   => 'meta_value',
                'compare' => '=',
            );
        }

        if($tax_query){
            $args['tax_query'] = $tax_query;
        }
        if($meta_query){
            $args['meta_query'] = $meta_query;
        }


        $the_query = new WP_Query( $args );
        $Return->AllPosts  = $the_query->posts;
        $Return->TotalData = $the_query->found_posts;
        $Return->LastPage  = $the_query->max_num_pages;
        
        return $Return;
    }

all rights reserved