Support

Account

Home Forums General Issues Taxonomy Issue

Solved

Taxonomy Issue

  • Hi Support,

    I’m hoping that you can help me work out this little conundrum…

    I have a Taxonomy custom field to allow the admin to select which category of a custom post type to display.

    I have then made this section to allow those posts to display. But for some reason its not displaying anything, i’m hoping that you can point out where I have gone wrong or show me where i’ve broken it.

    The code is as follows:

    <?php 
    $selected = get_field('system_contents_posts');
    $args=array(
    'post_type' => 'pj_systemcontents',
    'systemcontents_category' => "$selected");
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile; }?>
    <?php wp_reset_query(); ?>

    It just doesn’t display any posts at all.. I have a load of meta keys which need to be displayed from the posts themselves as well.. the above is just a test to see if I could get the title to show.

    Any help would be awesome!

    Many Thanks

    Chris

  • tried the below instead.. it displays the category information, but below the post (see the screenshot) which makes no sense, as it should be displaying it where it says Belgium, and then when you press the expander it will display the posts (the MEPs) underneath.

    <div id="accordion" class="panel-group">
        <div class="panel panel-default">
    <!-- SUB CATEGORY TITLE AREA -->
    <?php $member_group_terms = get_terms( 'meps_category' ); ?>
    <?php
    foreach ( $member_group_terms as $member_group_term ) {
        $member_group_query = new WP_Query( array(
            'post_type' => 'mep',
            'tax_query' => array(
                array(
                    'taxonomy' => 'meps_category',
    				'parent' => $parentcat,
                    'field' => 'slug',
                    'terms' => array( $member_group_term->slug ),
                    'operator' => 'IN',
                )
            )
        )
    	);
        ?>
            <div class="panel-heading">
    		<div class="col-md-2"><?php echo $member_group_term->category_image; ?></div>
    			<div class="col-md-10"><h3><?php echo $member_group_term->name; ?></h3>
    			<p><?php echo $member_group_term->description; ?></p></div>
    			<div class="clear bottomgap"></div>
    			<div class="col-md-8"></div><div class="col-md-4"><a data-toggle="collapse" data-parent="#accordion" class="button" href="#<?php the_ID(); ?>">View MEPs</a> <a href="#" class="button" target="_blank">Visit Website</a></div>
            <div class="clear"></div>
    		</div>
    <div class="clear"></div>
    <?php  if ( $member_group_query->have_posts() ) : ?>
    <!-- END SUB CATEGORY TITLE AREA -->
            <div id="<?php the_ID(); ?>" class="panel-collapse collapse col-md-12">
    <!-- SUB CATEGORY POSTS AREA -->
    		<?php while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
                <div class="panel-body">
    			<div class="col-md-2 meppic"><?php the_post_thumbnail(); ?></div>
    			<div class="col-md-10"><h3 class="mepname"><?php echo the_title();?></h3>
    			<strong><?php the_field('title_position'); ?></strong>
    			<div class="clear"></div>
    			<?php the_field('location'); ?>
    			<div class="clear"></div>
    			<br/>
                <?php the_field('description'); ?>
    			<div class="clear"></div>
    			<br/>
    			<div class="col-md-3 first"><strong>Email:</strong> <a href="mailto:<?php the_field('email'); ?>"><?php the_field('email'); ?></a></div><div class="col-md-3"><strong>Website:</strong> <a href="<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a></div><div class="col-md-3"><strong>Tel:</strong> <?php the_field('tel'); ?></div><div class="col-md-3 last"><a href="http://www.twitter.com/<?php the_field('twitter_id'); ?>" target="_blank"><?php the_field('twitter_id'); ?></a></div>
    			</div>
    			</div>
    <?php
    endwhile;
    endif;
    ?>
    <?php
        // Reset things, for good measure
        $member_group_query = null;
        wp_reset_postdata();
    }
    ?>
    <!-- END SUB CATEGORY POSTS AREA -->
            </div>
    
        </div>
    </div>

    Any help to fix this would be awesome!

  • You have some nesting issues in your code to work out.

    Near the bottom of what you posted, I commented were I found one nesting error. there may be others. I caught this by cleaning up the whitespace in your code.

    
    
            <?php  
              if ($member_group_query->have_posts()) :
                ?>
                  <!-- END SUB CATEGORY TITLE AREA -->
                  <div id="<?php the_ID(); ?>" class="panel-collapse collapse col-md-12">
                    <!-- SUB CATEGORY POSTS AREA -->
                    <?php 
                      while ($member_group_query->have_posts()) : 
                        $member_group_query->the_post(); 
                          // removed inner dive to make it easier to read
                      endwhile;
    									//there should be a </div> here before end if
                    endif;
    
  • that sooooo close!

    Its still showing the parent category at the top, which is wrong… but its now showing the child category below, which is right.

    <div id="accordion" class="panel-group">
        <div class="panel panel-default">
    <!-- SUB CATEGORY TITLE AREA -->
    <?php $member_group_terms = get_terms( 'meps_category' ); ?>
    <?php
    foreach ( $member_group_terms as $member_group_term ) {
        $member_group_query = new WP_Query( array(
            'post_type' => 'mep',
            'tax_query' => array(
                array(
                    'taxonomy' => 'meps_category',
    				'parent' => $currentCat,
                    'field' => 'slug',
                    'terms' => array( $member_group_term->slug ),
                    'operator' => 'IN',
                )
            )
        )
    	);
        ?>
            <div class="panel-heading">
    		<!--<div class="col-md-2"><img src="#" /></div>-->
    			<div class="col-md-12"><h3><?php echo $member_group_term->name; ?></h3>
    			<p><?php echo $member_group_term->description; ?></p></div>
    			<div class="clear bottomgap"></div>
    			<div class="col-md-8"></div><div class="col-md-4"><a data-toggle="collapse" data-parent="#accordion" class="button" href="#<?php the_ID(); ?>">View MEPs</a> <a href="#" class="button" target="_blank">Visit Website</a></div>
            <div class="clear"></div>
    		</div>
    <div class="clear"></div>
    <?php  if ( $member_group_query->have_posts() ) : ?>
    <!-- END SUB CATEGORY TITLE AREA -->
            <div id="<?php the_ID(); ?>" class="panel-collapse collapse col-md-12">
    <!-- SUB CATEGORY POSTS AREA -->
    		<?php while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
                <div class="panel-body">
    			<div class="col-md-2 meppic"><?php the_post_thumbnail(); ?></div>
    			<div class="col-md-10"><h3 class="mepname"><?php echo the_title();?></h3>
    			<strong><?php the_field('title_position'); ?></strong>
    			<div class="clear"></div>
    			<?php the_field('location'); ?>
    			<div class="clear"></div>
    			<br/>
                <?php the_field('description'); ?>
    			<div class="clear"></div>
    			<br/>
    			<div class="col-md-3 first"><strong>Email:</strong> <a href="mailto:<?php the_field('email'); ?>"><?php the_field('email'); ?></a></div><div class="col-md-3"><strong>Website:</strong> <a href="<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a></div><div class="col-md-3"><strong>Tel:</strong> <?php the_field('tel'); ?></div><div class="col-md-3 last"><a href="http://www.twitter.com/<?php the_field('twitter_id'); ?>" target="_blank"><?php the_field('twitter_id'); ?></a></div>
    			</div>
    			</div>
    <!-- END SUB CATEGORY POSTS AREA -->
            </div>
    		<?php
    endwhile;
    ?>
    <?php endif; ?>
    <?php
        // Reset things, for good measure
    	$member_group_query = null;
        wp_reset_postdata();
    }
    ?>
        </div>
    </div>
  • If you don’t want to shoe the parent category then you need to include $args for get_terms and set the parent term, then it won”t include those

    https://codex.wordpress.org/Function_Reference/get_terms

  • I’ve tried the below and also tried the ‘parent’ => 0.

    If I do the below, nothing happens. If I do the ‘parent’ => 0 it removes the subcategory and only shows the parent… there must be something i’m doing wrong 🙁

    <?php 
    $member_group_terms = get_terms( 'meps_category', array(
    	'child_of' => 0
     ) );
    ?>
    <?php
    foreach ( $member_group_terms as $member_group_term ) {
        $member_group_query = new WP_Query( array(
            'post_type' => 'mep',
            'tax_query' => array(
                array(
                    'taxonomy' => 'meps_category',
                    'field' => 'slug',
                    'terms' => array( $member_group_term->slug ),
    				'operator' => 'IN' 
                )
            )
        )
    	);?>
  • It is a hierarchical taxonomy?

    child of 0 will always return the top level. You need to use the ID of the top level category and it will only give you sub categories of that category.

  • right.. so I need the top level category to be dynamic for the one that you are currently viewing… I tried to do the following extra code to load this as current category:
    <?php $currentCat = $_GET[‘cat’];?>

    But its not working still…

    I have set as ‘parent’ => $currentCat within the get_terms array, but it now just displays the current category only (the Parent category).

    The weirdest thing is… is that the post that is displaying isn’t in the parent category at all (its not selected to anyway), its only in the child category.. so it should be displaying / appearing below the child category not the parent….

    Its not making much sense …

  • when i set the ‘child_of’ => $currentCat in the get_terms array… it just reverts to displaying both categories again..

  • I CRACKED IT…. it needed to be ‘hierarchical’ => false and it worked!!!!

  • Had to take a break for work. Glad you figured it out.

  • I think I spoke to soon.. Its 99% there.. I seem to have an issue now which is rather frustrating… I’m getting them to display correctly.. but now when I look in different categories (I have set up 2 different categories), i’m getting both the child categories display… even though they are from different parent categories..

    http://whyttip.eu/index.php/meps/bulgaria/
    http://whyttip.eu/index.php/meps/belgium/

    password: ecr_group

    for some strange reason they are both displaying even though one sub category (Bulgaria Without Censorship (България без цензура)) is for Bugaria and the other (New Flemish Alliance (Nieuw-Vlaamse Alliantie)) is for Belgium.

    The code i’m using is below. Can anyone help me work this out?

    <div class="blog-default">
    				<div class="master_container container">
    					<section class="page_content col-xs-12 col-md-12">
    						<div class="blog-default-bg">
    						
    						<h2>YOUR MEPS</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fringilla, orci ut mattis lacinia, enim risus hendrerit ante, fringilla fringilla turpis diam efficitur lectus. Donec sagittis, quam a pellentesque dictum, odio sapien porta eros, eget cursus nibh orci nec purus. Curabitur blandit tincidunt blandit. Morbi fermentum urna sed lacus condimentum scelerisque. Nunc vitae mauris sit amet lectus euismod accumsan.</p>
    <form id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
            <input class="inlineSearch" type="text" name="s" placeholder="Search" onblur="if (this.value == '') {this.value = 'Search';}" onfocus="if (this.value == 'Search') {this.value = '';}" />
    		<input type="hidden" name="post_type" value="mep" />
            <?php wp_dropdown_categories( 'show_option_none=Select Country&show_count=1&hierarchical=1&taxonomy=meps_category&depth=1' ); ?>
    		<?php wp_dropdown_categories( 'show_option_none=Select Group&show_count=1&hierarchical=0&taxonomy=meps_category&depth=1' ); ?>
    		<input class="inlineSubmit" id="searchsubmit" type="submit" alt="Submit" value="Submit" />
    </form>
    <br/>
    <?php $currentCat = $_GET['cat'];?>
    <?php $loop = new WP_Query(array('post_type'=>'mep', 'cat'=>$currentCat, 'order' => 'asc')); ?>
    <strong><?php echo single_cat_title();?> - Groups and MEPs:</strong>
    <div class="clear"></div>
    <br/>
    
    <div id="accordion" class="panel-group">
        <div class="panel panel-default">
    <!-- SUB CATEGORY TITLE AREA -->
    <?php 
    $member_group_terms = get_terms( 'meps_category', array(
    	'child_of' => $currentCat,
    	'hierarchical' => false
     ) );
    ?>
    <?php
    foreach ( $member_group_terms as $member_group_term ) {
        $member_group_query = new WP_Query( array(
            'post_type' => 'mep',
            'tax_query' => array(
                array(
                    'taxonomy' => 'meps_category',
                    'field' => 'slug',
                    'terms' => array( $member_group_term->slug ),
    				'operator' => 'IN' 
                )
            )
        )
    	);?>
            <div class="panel-heading">
    		<!--<div class="col-md-2"><img src="#" /></div>-->
    			<div class="col-md-12"><h3><?php echo $member_group_term->name; ?></h3>
    			<p><?php echo $member_group_term->description; ?></p></div>
    			<div class="clear bottomgap"></div>
    			<div class="col-md-8"></div><div class="col-md-4"><a data-toggle="collapse" data-parent="#accordion" class="button" href="#<?php echo $member_group_term->term_id; ?>">View MEPs</a> <a href="<?php echo $member_group_term->mep_party_link; ?>" class="button" target="_blank">Visit Website</a></div>
            <div class="clear"></div>
    		</div>
    <div class="clear"></div>
    
    <!-- END SUB CATEGORY TITLE AREA -->
            <div id="<?php echo $member_group_term->term_id; ?>" class="panel-collapse collapse col-md-12">
    <!-- SUB CATEGORY POSTS AREA -->
    <?php  if ( $member_group_query->have_posts() ) : ?>
    		<?php while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
                <div class="panel-body">
    			<div class="col-md-2 meppic"><?php the_post_thumbnail(); ?></div>
    			<div class="col-md-10"><h3 class="mepname"><?php echo the_title();?></h3>
    			<strong><?php the_field('title_position'); ?></strong>
    			<div class="clear"></div>
    			<?php the_field('location'); ?>
    			<div class="clear"></div>
    			<br/>
                <?php the_field('description'); ?>
    			<div class="clear"></div>
    			<br/>
    			<div class="mepdetails"><div class="col-md-3 first"><strong>Email:</strong> <a href="mailto:<?php the_field('email'); ?>"><?php the_field('email'); ?></a></div><div class="col-md-3"><strong>Website:</strong> <a href="http://<?php the_field('website'); ?>" target="_blank"><?php the_field('website'); ?></a></div><div class="col-md-3"><strong>Tel:</strong> <?php the_field('tel'); ?></div><div class="col-md-3 last"><a href="http://www.twitter.com/<?php the_field('twitter_id'); ?>" target="_blank"><?php the_field('twitter_id'); ?></a></div></div>
    			</div>
    			</div>
    <!-- END SUB CATEGORY POSTS AREA -->
    <?php
    endwhile;
    ?>
    </div>
    <?php endif; ?>
    <?php
        // Reset things, for good measure
    	$member_group_query = null;
        wp_reset_postdata();
    }
    ?> 
    </div>
    </div>
    </div>
    </section>
    </div>
    </div>
  • I’m still not exactly sure what you are doing?

    What template you’re using to display this?

    Can you go over again what you’re trying to show on the page?

    If you’re create a catagory named ‘meps_category’ then you can create a template file names `taxonomy-meps_category.php’, have you done this?

    I you do this then you can get the current category and then get only children of that category.

    
    $queried_object = get_queried_object();
    $terms = get_terms(get_terms(
        'meps_category', array(
        'parent' => $queried_object->term_id
    ));
    
  • Hi John.. I think that fixed it.. thank you so much!!

Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘Taxonomy Issue’ is closed to new replies.