Support

Account

Home Forums General Issues Help with displaying ACF relationship

Helping

Help with displaying ACF relationship

  • Hi, there. I have two custom post types: species and family.

    – In species I have this relational field “family_species” that filters by post type “family”.
    – In family post type I only have one fild (family_name);

    1 – So I created a new family called “green” and another one called “red”;
    2 – I created new ‘species’ entry called ‘alder’ (and selected ‘green’ family) and another one called ‘oak” (and selected ‘red’ family).

    I would like to display in a page a list for family (and once you click on the family name, it opens a html summary/detail with the species related to a specific family).

    – Green
    — Alder
    – Red
    — Oak

    My following code shows the families but something is not working when it comes to show only the species of each family. I’ve been trying everything for three days, but it’s not working. Can someone help me with that? thanks a lot.

        $my_args = array(
            'post_type' => 'family',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
        );
        $my_query = new WP_Query ($my_args);
        if( $my_query->have_posts()) : while ( $my_query->have_posts() ) : $my_query->the_post();?>
        <div>
            <div>
                <details>
                    <summary><h1><?php 
                        $family_name = get_field('family_name');
                        echo $family_name; ?>></h1><p></p>
                    </summary>
                    
                    <?php
                    $args = array(
                    'numberposts'	=> -1,
                    'post_type'=> 'species',
                    'meta_query'=> array(
                    'relation'=> 'AND',
                    array(
                    'key'=> 'specie_name',
                    'value'	=> '"' . get_the_ID() . '"',
                    'compare'=> 'LIKE'
                    ),
                    array(
                    'key'=> 'family_for_this_specie',
                    'value'	=> $family_name,
                    'type'	=> '='
                    )
                    )
                    );
    
                    // query
                    $the_queryy = new WP_Query( $args );
    
                    ?>
                    <?php if( $the_queryy->have_posts() ): ?>
                    <ul>
                    <?php while ( $the_queryy->have_posts() ) : $the_queryy->the_post(); ?>
                    <li>
                    <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                    </a>
                    </li>
                    <?php endwhile; ?>
                    </ul>
                    <?php endif; ?>
    
                    <?php wp_reset_query();	?>
                    
                </details>
            </div>
        </div>   
    
        <?php endwhile; endif; 
        wp_reset_query();?>
    

    Thak you so much for your attention.

  • “family_name” field is on the “family” post type and should not be in a query to get posts of post type “species”

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

You must be logged in to reply to this topic.