0
votes

I'm having some trouble displaying a list of posts from a Wordpress Category that will exclude a certain number of post based on a custom field using Advance Custom Fields.

Here's the current code I'm using that hides it nicely:

while ( have_posts() ) : the_post();
    $is_taken = get_field('taken_check', $this_id);
    if ($is_taken!=1) { 
        get_template_part( 'basket_selection' );
    } 
endwhile;

However, it simply just hides the post but still considers it as a post on the "posts_per_page" function.

For example, There are 20 posts in total and I've set the limit to 10 posts per page. If I hide 3 posts with the code above, it will only display 7 posts in page 1 and 10 posts in page 2.

Is there a way to simply just ignore the hidden posts and not count it as a "post"?

2

2 Answers

0
votes

Try this: Apply Custom Fields Parameters in get_post query itself.

$posts = get_posts(array(
    'posts_per_page' => 10,
    'post_type' => '<YOUR_POST_TYP>',
    'meta_key' => 'taken_check',
    'meta_value' => '<DEFAULT_VALUE_OF_taken_check>'
));

Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts

0
votes

I've managed to solve it by changing the get_posts to wp_query within the category.php.

I first added this code to detect the current category viewed and filter the query to only display taken_check = 0.

    $this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);

    foreach ($this_cat as $this_cat){
        $this_catid = $this_cat;
        break;
    }

    $args = array(
            'posts_per_page' => 10,
            'post_type' => 'post',
        'cat' => $this_catid,
        'orderby' => 'title',
        'order' => 'ASC',
        'paged' => $paged,
        'meta_query' => array(
            array(
                'key' => 'taken_check',
                'value' => '0',
            )
        )
     );

$wp_query = new WP_Query($args);

I then just continued with the default loop sequence. The only weird code is the unnecessary foreach loop to detect the current category based on the current page and not from a post. Still puzzled as to why I can't just use $this_cat[0] since it's an array. It keep returning blank.

Oh well, but it works now with pagination, so I'm happy :) Thanks for all the help!