Saturday, October 22, 2022
HomeWordPress Developmentphp - Weblog Posts not sorted and pagination not adjusted after filtering...

php – Weblog Posts not sorted and pagination not adjusted after filtering some class


Hey im actually caught and messing round now so i assumed i give a attempt to ask the neighborhood.

Im engaged on a Weblog Put up web page the place im displaying some posts. The consumer have the choice to seek for posts through a search kind or clicking on some classes and filtering the class. This works nice for now. However i could not discover a strategy to alter the pagination to this new filtered posts. Additionally the posts aren’t sorted after the filtering.

I created a brand new Array the place i loop by with none success. The posts are nonetheless on the identical place.

ex. Person is filtering for Outsourcing so all posts with class outsourcing are displaying however they’re splited this implies among the posts are on web page 1 a few of them are on web page 2 and so forth the array is just not sorted new after the filtering they usually persist with they web page positions:

right here is my code which i loop by then later and displaying the html with title, picture and many others… and a few screenshots:

$searchBlog = '';
$searchCategory = '';
if(!empty($_GET['searchBlog']))
{
    $searchBlog = $_GET['searchBlog'];

}
if(!empty($_GET['search_category']))
{
    $searchCategory = $_GET['search_category'];
}

world $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$classes = get_categories();

$args = array(
    'posts_per_page' => 5,
    'paged' => $paged,
    'post_type' => 'publish',
    'order' => 'DESC',
);

$huge = 999999999; // want an unlikely integer

$question = new WP_Query( $args );


$postsList = [];
foreach ($query->posts AS $publish)
{
    $customized = get_post_custom($post->ID);
    $post_category = wp_get_post_categories($post->ID);
    foreach ($post_category AS $cat)
    {
        $postsList[] = [
                'post' => $post,
                'category' => $cat,
        ];
    }
}
$filteredPostList = [];
foreach ($postsList AS $posts) {
    if (!empty($searchCategory) && $posts['category'] == $searchCategory) {
        $filteredPostList[] = $posts;
        var_dump('class');
    } elseif (!empty($searchBlog) && str_contains(strtolower($posts['post']->post_title), strtolower($searchBlog))) {
        $filteredPostList[] = $posts;
        var_dump('search');
    } elseif (empty($searchCategory) && $posts['category'] != $searchCategory && empty($searchBlog)) {
        $filteredPostList[] = $posts;
        var_dump('all');
    }
}

$pagination = paginate_links( array(
    'base' => str_replace( $huge, '%#%', esc_url( get_pagenum_link( $huge ) ) ),
    'format' => '/web page/%#%',
    'present' => max( 1, get_query_var('paged') ),
    'complete' => $query->max_num_pages,
    'prev_text'    => __('<i class="ico_arrow_left_black"></i>'),
    'next_text'    => __('<i class="ico_arrow_right_black"></i>'),
    'sort' => 'record',
) );

first page with only one post but posts per page is 5 and there are definitely more posts

here is the second page with more posts of the same category

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments