0

Hello im really stuck and messing around now so i thought i give a try and ask the community.

Im working on a Blog Post page where im showing some posts. The user have the selection to search for posts via a search form or clicking on some categories and filtering the category. This works fine for now. But i couldn't find a way to adjust the pagination to this new filtered posts. Also the posts are not sorted after the filtering.

I created a new Array where i loop through without any success. The posts are still on the same position.

ex. User is filtering for Outsourcing so all posts with category outsourcing are showing but they are splited this means some of the posts are on page 1 some of them are on page 2 and so on the array is not sorted new after the filtering and they stick to they page positions:

here is my code which i loop through then later and showing the html with title, image etc... and some screenshots:

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

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

global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$categories = get_categories();

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

$big = 999999999; // need an unlikely integer

$query = new WP_Query( $args );


$postsList = [];
foreach ($query->posts AS $post)
{
    $custom = 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('category');
    } 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( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '/page/%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $query->max_num_pages,
    'prev_text'    => __('<i class="ico_arrow_left_black"></i>'),
    'next_text'    => __('<i class="ico_arrow_right_black"></i>'),
    'type' => 'list',
) );

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

2 Answers 2

2

I found my own Answer:

due to my second array which i created to add some categories the filter only searched for each page separately. with 'cat' i could search for the category and with 's' i could search for a specific value like the title. so i put everything i needed in $args to make it work :

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

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

global $wp_query;
$categories = get_categories();

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'posts_per_page' => 12,
    'paged' => $paged,
    'post_type' => 'post',
    'order' => 'DESC',
    'cat' => $searchCategory,
    's' => $searchBlog,

);


$query = new WP_Query( $args );
1

Depending on how many categories you will have, I would create result pages for each category. I would do the filtering with the GET method as you already did. And it would use a switch case to select each result page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.