• Resolved drushton

    (@drushton)


    Hi,

    I have a custom post type “portfolio” and all portfolio items can belong to one or more categories.

    I wish to display all portfolio items that belong to a specific category but the portfolio items can belong to other categories as well.

    Currently, the page displays all portfolio items and there are two instances where the portfolio items are identified:

    Query 1:

    <?php $catorg = get_post_meta($post->ID, 'portfolio_categories', TRUE); ?>
    		<?php $cat = implode(',', $catorg); ?>
    		<div class="twelve columns">
    			<h4><?php _e( 'Filter', THB_THEME_NAME ); ?></h4>
    			<ul class="filters hide-for-small">
    			  <li><a href="#" data-filter="*" class="active"><?php _e( 'show all', THB_THEME_NAME ); ?></a></li>
    			  <?php
    				$portfolio_categories = get_categories(array('taxonomy'=>'project-category', 'include' => $cat));
    				foreach($portfolio_categories as $portfolio_category) {
    					$args = array(
    					    'post_type' => 'portfolio',
    					    'post_status' => 'published',
    					    'project-category' => $portfolio_category->slug,
    					    'numberposts' => -1
    					);

    Query 2:

    <?php $args = array(
    	    	   'post_type' => 'portfolio',
    	    	   'orderby'=>'menu_order',
    	    	   'order'     => 'ASC',
    	    	   'posts_per_page' => '-1',
    	    	   'tax_query' => array(
    	    	   		array(
    		           'taxonomy' => 'project-category',
    		           'field' => 'id',
    		           'terms' => $catorg,
    		           'operator' => 'IN'
    	    	      )
    	    	    ) // end of tax_query
        	  	);
    		?>
    		<?php $query = new WP_Query($args); ?>
                <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
                      $terms = get_the_terms( get_the_ID(), 'project-category' );
                      $type = get_post_meta($post->ID, 'portfolio_type', true);

    Essentially, how do I add a condition to the $args or elsewhere that ensures that the posts returned belong to a specific category using the category ID but also can belong to other categories.

    Thank you in advance for your time.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter drushton

    (@drushton)

    p.s. I assume it is a tax_query for query 2 but not sure about query 1. I haven’t been able to get it to work however.

    Also, Query 1 feeds into a category selector using a data-filter. Not sure how this affects things if at all?

    Thread Starter drushton

    (@drushton)

    So changing Query 2 args using a custom tax query to:

    <?php $args = array(
    	    	   'post_type' => 'portfolio',
    	    	   'orderby'=>'menu_order',
    	    	   'order'     => 'ASC',
    	    	   'posts_per_page' => '-1',
    	    	   'tax_query' => array(
    					'relation' => 'AND',
    						array(
    					   'taxonomy' => 'project-category',
    					   'field' => 'id',
    					   'terms' => $catorg,
    					   'operator' => 'IN'
    					  ),
    					  array(
    					   'taxonomy' => 'project-category',
    					   'field' => 'id',
    					   'terms' => 37
    					  )
    	    	    ) // end of tax_query
        	  	);
    		?>

    fixes the issue with Query 2.

    Now onto Query 1…

    Thread Starter drushton

    (@drushton)

    Same fix for query 1:

    foreach($portfolio_categories as $portfolio_category) {
    			     	$args = array(
    							'post_type' => 'portfolio',
    						   'posts_per_page' => '-1',
    						   'tax_query' => array(
    								'relation' => 'AND',
    									array(
    								   'taxonomy' => 'project-category',
    								   'field' => 'slug',
    								   'terms' => $portfolio_category->slug
    								  ),
    								  array(
    								   'taxonomy' => 'project-category',
    								   'field' => 'id',
    								   'terms' => 37
    								  )
    							) // end of tax_query
    					);

    This has however broken the data-filters

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display posts belonging in 1 category as well as others’ is closed to new replies.