• Greetings friends, I’m a cruel doubt how to delete only one category of posts related to my topic, I’ve tried everything I found on the internet but it did not work out, I would like to exclude from the listings only category whose id 12. how to do? follows the code. Dese ja appreciate the help of todos.Obrigado

    <?php
    
    global $get_meta , $post;
    
    if( ( tie_get_option('related') && empty( $get_meta["tie_hide_related"][0] ) ) || ( isset( $get_meta["tie_hide_related"][0] ) && $get_meta["tie_hide_related"][0] == 'no' ) ):
    	$related_no = tie_get_option('related_number') ? tie_get_option('related_number') : 3;
    
    	global $post;
    	$orig_post = $post;
    
    	$query_type = tie_get_option('related_query') ;
    	if( $query_type == 'author' ){
    		$args=array('post__not_in' => array($post->ID),'posts_per_page'=> $related_no , 'author'=> get_the_author_meta( 'ID' ));
    	}elseif( $query_type == 'tag' ){
    		$tags = wp_get_post_tags($post->ID);
    		$tags_ids = array();
    		foreach($tags as $individual_tag) $tags_ids[] = $individual_tag->term_id;
    		$args=array('post__not_in' => array($post->ID),'posts_per_page'=> $related_no , 'tag__in'=> $tags_ids );
    	}
    	else{
    		$categories = get_the_category($post->ID);
    		$category_ids = array();
    		foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    		$args=array('post__not_in' => array($post->ID),'posts_per_page'=> $related_no , 'category__in'=> $category_ids );
    	}
    	$related_query = new wp_query( $args );
    	if( $related_query->have_posts() ) : $count=0;?>
    
    	<section id="related_posts">
    		<div class="block-head">
    			<h3><?php _e( 'Related Articles' , 'tie' ); ?></h3><div class="stripe-line"></div>
    		</div>
    		<div class="post-listing">
    			<?php while ( $related_query->have_posts() ) : $related_query->the_post()?>
    			<div class="related-item">
    				<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>
    				<div class="post-thumbnail">
    					<a href="<?php the_permalink(); ?>" title="<?php printf( __( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
    						<?php tie_thumb('', 272 ,125); ?>
    						<span class="overlay-icon"></span>
    					</a>
    				</div><!-- post-thumbnail /-->
    				<?php endif; ?>
    				<h3><a href="<?php the_permalink(); ?>" title="<?php printf( __( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    				<p class="post-meta"><?php the_time(get_option('date_format')); ?></p>
    			</div>
    			<?php endwhile;?>
    			<div class="clear"></div>
    		</div>
    	</section>
    	<?php	endif;
    	$post = $orig_post;
    	wp_reset_query();
    endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    My understanding is you want to remove the 12 if it exists in $category_ids. You can use array_diff() for this. Insert between the foreach and $args lines:
    $category_ids = array_diff( $category_ids, array( 12 ));

    Off topic: “cruel doubt” – this is not a phrase we hear in American English. Maybe it is heard in other English speaking cultures, or maybe it just does not translate well. Either way, I like it! I’m going to try to use it in conversation ??

    Thread Starter MaxSan

    (@maxsan)

    bcworkz hello thanks for your reply sorry the mistake of English I used google translator.
    I added the line of code but did not work, I do not want appears in the posts related the category id 12 .

    Moderator bcworkz

    (@bcworkz)

    No need to apologize for bad translations like “cruel doubt”, it is entertaining and yet the meaning is clear ??

    There are a few explanations for this not working. My modification only applies to pages that are not author or tag queries (which I will call “other” pages). If you need this restriction for author or tag queries, we need to modify their respective arguments. I only modified the “other” arguments.

    For other pages, the query should not be returning any posts that are only in category 12, but if there are other categories in the $category_ids array attached to a post in addition to 12, the post is still returned. Is this what is happening? To solve this, we need to modify the “other” arguments.

    Or are you getting posts returned on other pages for which the only category is 12? If this is the issue, some different script must be modifying the query. This could take some work to resolve.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how exclude one category of lists’ is closed to new replies.