Support

Account

Home Forums General Issues Using Taxonomy to display custom posts

Solving

Using Taxonomy to display custom posts

  • Hi again everyone,

    I have another weird one.. I’m hoping that someone can help figure out.

    I have a custom post type which is populated with a load of information, its mostly within meta fields, as its a dynamic import from an external feed which is auto updated every week, this is irrelevant to the problem, but a bit of info 🙂

    I have used some other code from a previous problem that I had (which was solved yesterday, YAY) but I can’t seem to get over this one hurdle.. which is using the Taxonomy select option from the custom field.

    What i’m trying to do is allow the admin to select which posts to display from the backend in the Taxonomy field to then display within a loop that I created on the frontend. Simple enough I would’ve thought..

    Here is the code i’m using:

    `<?php
    $member_group_terms = get_terms( ‘systemcontents_category2’);
    $selected = get_sub_field(‘system_contents_posts’);
    ?>
    <?php
    foreach ( $member_group_terms as $member_group_term ) {
    $member_group_query = new WP_Query( array(
    ‘post_type’ => ‘pj_systemcontents2’,
    ‘systemcontents_category2’ => $selected,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘systemcontents_category2’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( $member_group_term->slug ),
    ‘operator’ => ‘IN’
    )
    )
    )
    );?>
    <?php if ( $member_group_query->have_posts() ) : ?>
    <h3><?php echo $member_group_term->name; ?></h3>

      <?php while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>

    • <?php echo get_post_meta($post->ID,’document-section’,true) ?>
      • <?php echo get_post_meta($post->ID,’document-reference’,true) ?> <?php echo get_post_meta($post->ID,’document-sub-section’,true) ?>
    • <?php endwhile; ?>

    <?php endif; ?>
    <?php
    // Reset things, for good measure
    $member_group_query = null;
    wp_reset_postdata();
    }
    ?>’

    The $selected = get_sub_field(‘system_contents_posts’); should be referencing to the page’s custom field selection for the categories to display, in the backend it is allowing me to choose the categories, but the front end is just showing ALL posts..

    Also annoyingly the <?php echo get_post_meta($post->ID,’document-section’,true) ?> section which is supposed to be showing the post meta for the sub section, isn’t working either.. Its not entirely out of the loop.. so I was hoping that it would just pick it up.

    So ultimately I have 2 issues:

    1. The selection isn’t working
    and
    2. How to get the second title to display. Ideally this will group the items under the same name.. if thats possible. Please see the 2 attached screenshots (screenshot1 is how it currently displays, screenshot2 is how it ideally needs to display)

    I hope that someone can help me?

    Thanks in advance

    Chris

  • Hi @chrismitchell

    Please put all your code inside code tags so it’s readable 😉

  • Terribly Sorry… don’t know why it didn’t work…

    <?php
    $member_group_terms = get_terms( ‘systemcontents_category2′);
    $selected = get_sub_field(‘system_contents_posts’);
    ?>
    <?php
    foreach ( $member_group_terms as $member_group_term ) {
    $member_group_query = new WP_Query( array(
    ‘post_type’ => ‘pj_systemcontents2′,
    ‘systemcontents_category2′ => $selected,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘systemcontents_category2′,
    ‘field’ => ‘slug’,
    ‘terms’ => array( $member_group_term->slug ),
    ‘operator’ => ‘IN’
    )
    )
    )
    );?>
    <?php if ( $member_group_query->have_posts() ) : ?>
    <h3><?php echo $member_group_term->name; ?></h3>
    
    <?php while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
    <?php echo get_post_meta($post->ID,’document-section’,true) ?>
    <?php echo get_post_meta($post->ID,’document-reference’,true) ?> <?php echo get_post_meta($post->ID,’document-sub-section’,true) ?>
    <?php endwhile; ?>
    
    <?php endif; ?>
    <?php
    // Reset things, for good measure
    $member_group_query = null;
    wp_reset_postdata();
    }
    ?>
  • Hey!

    So you’re sort of twisting and turning the query around. You start off with terms which you use to query posts which then are used to show terms.

    Your code is a bit confusing.. I’ll post a cleaned up version here but first I want to talk about what it does.

    So with the current code you fetch ALL terms in the taxonomy systemcontents_category2. You then loop through each term and perform a wp_query which fetches all posts connected to that term.

    Then you loop through each post and output the terms name and some information from the post.

    Nowhere is $selected being used and I don’t quite get what it is you want to use it for either. You should also be using ACFs API functions like get_field() to fetch the meta values for the post.

    If this is not what you need please try to explain further and possibly include a screenshot of your ACF setup.

    
    <?php
    $member_group_terms = get_terms('systemcontents_category2');
    $selected = get_sub_field('system_contents_posts');
    ?>
    <?php foreach ( $member_group_terms as $member_group_term ) endforeach; ?>
    
    	<?php
    	$args = array(
    		'post_type' => 'pj_systemcontents2',
    		'posts_per_page' => 400, //unlikely high
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'systemcontents_category2',
    				'field' => 'slug',
    				'terms' => array( $member_group_term->slug ),
    				'operator' => 'IN'
    			)
    		)
    	);
    	$member_group_query = new WP_Query($args);
    	?>
    	<?php if ( $member_group_query->have_posts() ) : ?>
    		<h3><?php echo $member_group_term->name; ?></h3>
    		<?php while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
    			<?php echo get_field('document-section'); ?>
    			<?php echo get_field('document-reference') . ' ' . get_field('document-sub-section'); ?>
    		<?php endwhile; wp_reset_postdata(); ?>
    	<?php endif; ?>
    	
    <?php endforeach; ?>
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Using Taxonomy to display custom posts’ is closed to new replies.