• Resolved stuartcr

    (@stuartcr)


    Hi people,

    Im struggling with taxonomies and taxonomy terms not returning what im expecting.

    this is my code:

    <?php if( get_row_layout() == 'post_grid' ):?>
                <div class="post-grid">
                <style>
                .post-grid {
        display: grid; gap: 2%; grid-template-columns: repeat(<?php the_sub_field('post_per_row'); ?>,minmax(0,1fr));}.single-item a img {width: 100%; height: <?php the_sub_field('post_image_height')?>; object-fit: cover;}</style>
                <?php 
                $PostTypeChoice = get_sub_field('post_type_choice');
                $PostsPerPage = get_sub_field('post_per_page');
                $categoryTag = get_sub_field('category_tag');
                $categoryTagTerm = get_sub_field('category_tag_term');
                $postGridArgs = array(
                    'post_type' => $PostTypeChoice,
                    'posts_per_page' => $PostsPerPage,
                    'post_status' => 'publish',
                    'tax_query' => array(
                            array(
                                'taxonomy' => $categoryTag,
                                'field'    => 'name',
                                'terms'    => $categoryTagTerm,
                                'operator' => 'IN'
                            )
                    )
            );
        $postGridItems = new WP_Query($postGridArgs);
                while($postGridItems -> have_posts()): $postGridItems -> the_post();
            ?> 
                <div class="single-item">
                    <?php if(get_sub_field('with_image') == '1'):?><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium'); ?></a><?php endif;?><a href="<?php the_permalink(); ?>"><h2><?php the_title();?></h2></a><a href="<?php the_permalink(); ?>"><span>Read More..</span></a></div>
               </div>
            <?php endwhile; wp_reset_query(); ?>
                <?php endif; ?>

    Im trying to create simple customisable post grid.

    So <?php echo (get_sub_field('category_tag_term'));?> returns “New Cat, Uncategorised” exactly what i want.
    BUT
    echo $categoryTagTerm; returns just the word “array” not what i want.

    so if im using this $categoryTagTerm = get_sub_field('category_tag_term');

    why are they not the same?

    when you try loading the page linked above im just displaying what is in the variables using “echo” and “print_r”

    im just so confused because the data i want is there, but its not working.

    what am i doing wrong?

    Help please!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The Taxonomy Terms field has 3 different “Return Format” settings possible:

    • Terms names
    • Terms ID
    • Terms object

    In all cases, it will return an array of Terms Names/ID/Object when using get_field('my_taxonomy_terms'), depending on the “Return Format” you selected in the Field Settings.

    I’m not sure why you see the following:

    So <?php echo (get_sub_field(‘category_tag_term’));?> returns “New Cat, Uncategorised” exactly what i want.

    This is not normal, as get_field() and get_sub_field() will never change an array into a readable string format such as Item 1, Item 2.

    However, the ACF functions the_field() and the_sub_field() which echo values has a builtin feature which convert array into a readable string such as this one.

    In all cases, your code looks quite complexe to debug the issue. I would recommend to run a simple test to see which function return what:

    <?php if(have_rows('my_flexible_content')): ?>
        <?php while(have_rows('my_flexible_content')): the_row(); ?>
        
            <?php if(get_row_layout() === 'post_grid'): ?>
            
                get_sub_field:
                <pre><?php print_r(get_sub_field('category_tag_term')); ?></pre>
            
                <hr/>
    
                the_sub_field:
                <pre><?php the_sub_field('category_tag_term'); ?></pre>
            
            <?php endif; ?>
        
        <?php endwhile; ?>
    <?php endif; ?>
    

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter stuartcr

    (@stuartcr)

    Hi Konrad,

    thank you so much for getting back to me.

    I have implemented the simpler code you suggested

    looks like i have used the incorrect function the explain my issue the_sub_field rather than get_sub_field.

    thank you for explaining the difference.

    what i need is for
    “$categoryTagTerm” to be replaced by “array(‘New Cat, Uncategorised’)”

    so that the chosen terms are placed with in the sub-array

    the same way “$PostTypeChoice” becomes the chosen post-type ie “testimonials”
    and
    “$PostsPerPage” becomes the chosen number of post to display for example “4”

    this

    $postGridArgs = array(
                    'post_type' => $PostTypeChoice,
                    'posts_per_page' => $PostsPerPage,
                    'post_status' => 'publish',
                    'tax_query' => array(
                      array(
                           'taxonomy' => $categoryTag,
                           'field'    => 'name',
                           'terms'    => $categoryTagTerm,
                            'operator' => 'IN'
                            )
                    )
            );

    to this

    $postGridArgs = array(
                    'post_type' => 'testimonials',
                    'posts_per_page' => '4',
                    'post_status' => 'publish',
                    'tax_query' => array(
                      array(
                           'taxonomy' => 'category',
                           'field'    => 'name',
                           'terms'    => array('New Cat, Uncategorised'),
                            'operator' => 'IN'
                            )
                    )
            );

    maybe there is a way to get the data i want, i assume its a string of values separated by commas, that creates the sub array?

    I just got $PostTypeChoice and $PostsPerPage functioning so quick. Perhaps im approaching it all wrong, sorry im very new to this

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    In order to turn an array into a string which use commas, I would recommend to check the implode() php function (see documentation).

    However, note that’s not how the tax_query works, as it expect an array of terms ids/names/slugs if you want to query multiple terms, not an array with a string using commas. See WP_Query documentation & demo.

    Notice the difference:

    Array of values (good):

    array('value1', 'value2')
    

    Array with a string that use commas (not good):

    array('value1, value2')
    

    Hope it helps!

    Have a nice day!

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘struggling with taxonomies and taxonomy terms’ is closed to new replies.