WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hide specific posts from category list (10 posts)

  1. scnix
    Member
    Posted 11 months ago #

    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. Rod Whiteley
    Member
    Posted 11 months ago #

    I recently did something similar. You have to modify the query before paging occurs.

    Look in wp-includes/query.php for function get_posts() and you'll see the available filters that you can use before the "// Paging" comment.

    I used 'pre_get_posts' to add a 'posts_where' filter for one particular category, removing the filter for any other kind of query.

  3. scnix
    Member
    Posted 11 months ago #

    Wow, thanks. I didn't expect to look into the include files.

    Anyways, if I were to update wordpress, will this file be updated and set to default as well?

    Also, after trying it out, I don't seem to understand how to include those methods. Can you please provide me an example?

  4. Rod Whiteley
    Member
    Posted 11 months ago #

    I only suggested that you look in the file to see what's there. As you say, it would be a bad idea to modify any file in wp-includes, because an update to WordPress would destroy your modifications.

    I don't have time to provide an example just now. Maybe later today or at the weekend I'll try to find the code I used.

  5. scnix
    Member
    Posted 11 months ago #

    I've been trying to experiment with the get_posts($args) but that will screw up the category.php posts.

    Is there a way to not query a new post list but still filter by the custom field within the category.php?

  6. keesiemeijer
    moderator
    Posted 11 months ago #

    See:
    http://www.billerickson.net/customize-the-wordpress-query/
    http://codex.wordpress.org/Function_Reference/WP_Query

    Try it with this in your functions.php:

    function my_category_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		if ( is_category() ) { // all category pages
    			$meta_query = array(
    				array(
    					'key' => 'taken_check',
    					'value' => 1,
    					'compare' => '!=',
    					'type' => 'numeric',
    				)
    			);
    			$query->set( 'meta_query', $meta_query );
    		}
    	}
    }
    
    add_action( 'pre_get_posts', 'my_category_queries' );

    I'm not sure this will work with the Advances Custom Fields plugin.

    btw:
    consider creating a child theme instead of editing your theme directly - if you upgrade the theme all your modifications will be lost.

  7. scnix
    Member
    Posted 11 months ago #

    Unfortunately that doesn't filter the category posts. It still lists them all.

    I did some really weird code that "sort of" worked, but it didn't allow paging.

    $this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);
    
    foreach ($this_cat as $this_cat){
    	$this_catid = $this_cat; // for some reason I couldn't just use $this_cat[0], so in order to get the first array list, I did this
    	break;
    }
    
    $posts = get_posts(array(
        	'posts_per_page' => 10,
        	'post_type' => 'post',
    	'category' => $this_catid,
    	'orderby' => 'title',
    	'order' => 'ASC',
       	'meta_query' => array(
    		array(
    			'key' => 'taken_check',
    			'value' => '0',
    		)
    	)
    ));

    That did filter the category post nicely, but it it keeps displaying page 1 only.

  8. keesiemeijer
    moderator
    Posted 11 months ago #

    Pagination doesn't work with get_posts().
    Can you try using your meta query in my code (in functions.php):

    function my_category_queries( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		if ( is_category() ) {
    			$meta_query = array(
    				array(
    					'key' => 'taken_check',
    					'value' => '0',
    				)
    			);
    			$query->set( 'meta_query', $meta_query );
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_category_queries' );
  9. scnix
    Member
    Posted 11 months ago #

    Tried it, but it still doesn't filter on the category pages.

    The code I placed was in category.php, which could be the reason why the meta query works there?

  10. scnix
    Member
    Posted 11 months ago #

    Hi, 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!

Reply

You must log in to post.

About this Topic