Support

Account

Home Forums General Issues Get posts by taxonomy field

Unread

Get posts by taxonomy field

  • Hello

    I’ve been using this great plugin for a while now but first time I need some advice.

    I have regular WP blog posts and I want to display these posts on WooCommerce single product page depending on product category. For example I have post “Laptop reviews” and it should be displayed in all product pages if product category is “Laptops” and for example second post is “Computer reviews”, it should be displayed in all product pages if product category is “Computers”. This is simplified example, I have lot of posts and I need them to show up in right product pages depending on product category.

    I’m using Taxonomy field type (Taxonomy is product category) related with WP posts so in each post I select product categories using Multiselect appearance.

    To display posts in product pages, I’m using following code

    
    $cat = get_queried_object();
    $catID = $cat->term_id;
    	$posts = get_posts(array(
    		'numberposts'	=> -1,
    		'post_type'		=> 'post',
    		'meta_query' => array(
                          array(
                                'key' => 'tooted', 
                                'value' => $catID, 
                                'compare' => 'LIKE'
                                    )
    								)
    	));
    
    	if( $posts ): ?>
    	
    
    	
    			<ul>
    		
    				<?php foreach( $posts as $post ): 
    		
    				setup_postdata( $post );
    		
    				?>
    					<li>
    
    					<?php echo get_the_title(); ?>
    				
    					</li>
    	
    				<?php endforeach; ?>
    	
    				</ul>
    	
    				<?php wp_reset_postdata(); ?>
    
    			<?php endif; ?>
    

    Somehow it displays post titles in all products pages, not only in product pages which categories I have selected. And somehow, if I deselect category, it still keeps displaying the post title.

    Does anyone has idea, how to make this work?

    Thanks in advance!
    Toomas

Viewing 1 post (of 1 total)

The topic ‘Get posts by taxonomy field’ is closed to new replies.