• Hello,

    I’ve been looking for the solution to this problem for hours … unfortunately I have not found.

    After creating a “custom post type” (with taxonomy) – I created a group of fields to the taxonomy (for this taxonomy only).

    After several unsuccessful searches, I ask my question here. Indeed, I’m trying to display a list of posts grouped by categories:

    CAT A

    post1
    post2
    post3
    CAT B

    post4
    post5
    post6
    post7 ..

    Someone could help me please?

    Custom post type: client
    Name of taxonomy: secteur
    Custom field on taxonomy: secteur_dactivite

    
    <?php 
            $terms = get_terms( 'secteur', array(
                'orderby'    => 'count',
                'hide_empty' => 0
            ) );
            
            foreach( $terms as $term ) {
                $args = array(
                    'post_type' => 'client',
                    'posts_per_page' => '-1',
                  'secteur' => $term->slug
                );
                $query = new WP_Query( $args );
      
                echo'<h3>' . $term->name . '</h3>';
               
    
                    // Start the Loop
                    while ( $query->have_posts() ) : $query->the_post(); 
                    $secteur_dactivite = get_field( 'secteur_dactivite' );
    
                    echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
    
                     endwhile;
    
                wp_reset_postdata();
     
            } 
        
        ?>
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • WP_Query accepts a tax_query as the argument to narrow down posts by terms. It’s a nested array like so:

    $args = array(
    	'post_type' => 'client',
    	'posts_per_page' => '-1',
    	'tax_query' => array( array(
    		'taxonomy'	=> 'secteur',
    		'field'		=> 'slug',
    		'terms'		=> $term->slug,
    	) ),
    );

    For more information, see the examples under Taxonomy, post by term via WP_Query

    Thread Starter creatz

    (@creatz)

    Hello,
    thank you very much for your response.
    Unfortunately, this does not change the display. The titles are displayed but not the articles.

    <h3>CAT1</h3
    <h3>CAT2</h3>
    <h3>CAT3</h3>

    If it helps, I can display all the items with the following code :

     <?php
                $posts = get_posts(array(
                    'numberposts' => -1,
                    'post_type' => 'client',
                    'post_status' => 'publish'
                ));
                if($posts)
                {
                    echo '<div class="row all-item">';
                    foreach($posts as $post)
                    {
                        echo '<div"><a href="'. get_permalink($post->ID) .'"><img src="'.get_field( 'logo' ).'"></a></div>';
                    }
                    echo '</div>';
                }
            ?>

    You could pass the same WP_Query arguments from my previous comment into the get_posts() function as well. The loop looks fine. Are you sure that the taxonomy and the post type are correct? Have you enabled debugging to see if there are any PHP errors preventing it from displaying? Can you test if you can get inside the while() loop or if the query returns no results?

    Moderator bcworkz

    (@bcworkz)

    The titles are displayed but not the articles.

    If you want article content displayed as well, you need to add code to output post content. Try adding the_content(); after title output.

    If you want template tags like the_content() and get_field() to work within a foreach loop, first declare $post global before entering the loop. In the loop, call setup_postdata( $post );

    After the loop completes, call wp_reset_postdata();

    Thread Starter creatz

    (@creatz)

    Thank you very much for your answers.
    But nothing has worked for me, and I can’t come up with any solutions.
    I think the problem lies in the configuration of my taxonomy.

    This is my custom post type configuration (client):

    View post on imgur.com

    my taxonomy configuration (secteur) :

    View post on imgur.com

    my custom field (secteur_dactivite) :

    View post on imgur.com

    Thanks again for your help

    Where and/or what template file are you trying to put this code into?

    Thread Starter creatz

    (@creatz)

    I think I’ve found the solution!

    It seems that the field “Secteur d’activité” is not linked to the category “Secteur”.

    View post on imgur.com

    Moderator bcworkz

    (@bcworkz)

    Indeed! Fields are saved as meta data, taxonomy terms are managed independently in a complex interrelated table scheme. I wrongly assumed your use of the word “field” in relation to taxonomies was a translation issue and not literally a custom field. Apologies.

    To get posts with a specific custom field value, use the “meta_key” and “meta_value” arguments to WP_Query class. For more complex field criteria you can use “meta_query” instead.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Taxonomy, post by term’ is closed to new replies.