Question

I have a CPT 'formations'. In the page of one of my formations (single.php) there is a section "other articles". I would like from my editor of each "formation" page, select categories to show other "formation" desired. The list of my taxonomies is created. For the moment they are in the same archive.php appearance structure (they all appear) with a loop.

Conclude: on my "formation" page I want to be able to check the categories of articles that must appear in the block "other formation" on my page.

how should i place and or my taxonomy ACF loop for my post to appear. For moment i created the metabox with ACF field type: Taxonomy & name of my taxonomy "Thematiques"

My code: single-formation.php

<ul>
                <?php $args = array('post_type' => 'formation');?>
            <?php $loop = new WP_Query($args);?>
                <?php while ($loop -> have_posts()): $loop->the_post();?>
                <li>
                    <?php the_post_thumbnail('cpt_formation');?>
                    <div class="container-other">
                        <h4><?php the_title();?></h4>
                        <p><?php the_content();?>
                        </p>
                        <a href="<?php the_field('url_formation');?>">En savoir plus</a>
                    </div>  
                </li>
                <?php endwhile;?>
            </ul>

My code: functions.php

    function custom_formation(){
    $labels = array(
        'name' => 'Formation',
        'all_items' => 'Toutes les formations',  // affiché dans le sous menu
        'singular_name' => 'Formation',
        'add_new_item' => 'Ajouter une formation',
        'edit_item' => 'Modifier la formation',
        'menu_name' => 'Formation'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_in_rest' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor','thumbnail' ),
        'menu_position' => 5, 
        'menu_icon' => 'dashicons-format-status',
    );
    register_post_type( 'formation', $args );
    enter register_taxonomy(
        'thematique',
        'formation',
        array(
            'label' => 'Thématiques',
            'labels' => array(
            'name' => 'Thématiques',
            'singular_name' => 'Thématiques',
            'all_items' => 'Toutes les Thématiques',
            'edit_item' => 'Éditer la Thématique',
            'view_item' => 'Voir la Thématiques',
            'update_item' => 'Mettre à jour la Thématiques',
            'add_new_item' => 'Ajouter une Thématiques',
            'new_item_name' => 'Nouvelle Thématiques',
            'search_items' => 'Rechercher parmi les Thématiques',
            'popular_items' => 'Thématiques les plus utilisés',

        ),
            'hierarchical' => true,
            'show_in_rest' => true,
            'rewrite'=>false,
        )
    );
}
add_action('init','custom_formation');
Was it helpful?

Solution

You would want to use the value of the ACF Taxonomy Field as an additional argument of your WP_Query. So in 'single-formation.php' replace your WP_Query with something like this:

$term_ids = get_field('taxonomy_field_name'); // make sure the return value of your ACF Taxonomy field is set to "Term ID"

$args = array(
    'post_type' => 'formation',
    'tax_query' => array(
        array(
            'taxonomy' => 'thematique',
            'field'    => 'term_id',
            'terms'    => $term_ids,
        ),
    ),
);
$loop = new WP_Query( $args );

$loop should now contain any CPT 'formations' with the selected terms of the 'thematique' taxonomy.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top