admin管理员组

文章数量:1414628

I've run into something very odd that I don't think I've ever seen before. I'm trying to query a custom-post-type and organize the results into groups by a custom taxonomy term. The post is Project and they're organized by a taxonomy of State.

The problem is that all of the States are printing out, but some of them don't display any associated Projects (even thought they have some, switching hide_empty between true and false produces expected results). I've tried a few combinations of term names and it appears that this only happens when the taxonomy term has a two-word name: e.g. "North Dakota". If I change the State on a Project to a different (single-named) State, the data does show.

Normally this looks something like:

State 1

  • Project
  • Project
  • Project

State 2

  • Project
  • Project

But some of the states are only displaying their name (taxonomy term name) and no Projects underneath.

State 3

State 4

I noticed the slugs of the two-named states had been concatenated into one word (northdakota), so I'm not sure if that could somehow be causing this. I tried reverting to north-dakota with no luck. Is there somewhere these exist in the db after you delete them that's maybe conflicting? Terms don't seem to have a Trash page.

This is the first project I've built using Gutenberg and ACF Gutenblocks but I don't see how that could be causing this, especially since all the data I'm querying is part of Core for this particular block.

Am I just missing something, or is this some sort of bug?

<?php
$states = get_terms( array(
    'taxonomy'   => 'state',
    'hide_empty' => true,
) );
foreach ( $states as $state ) {
    $state_name = $state->name;
    $state_slug = $state->slug;
    ?>
    <div class="state <?php echo esc_attr( $state_slug ); ?> mix">
        <h3 class="state-heading"><?php echo esc_attr( $state_name ); ?></h3>
        <?php
        $args = array(
            'post_type'      => 'project',
            'posts_per_page' => - 1,
            'orderby'        => 'title',
            'order'          => 'ASC',
            'tax_query'      => array(
                array(
                    'taxonomy' => 'state',
                    'field'    => 'name',
                    'terms'    => $state_slug,
                ),
            ),
        );
        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <div class="project">
                <div class="row">

                    <div class="col-md-6 d-flex flex-column justify-content-center">
                        <?php
                        $title     = get_the_title();
                        $excerpt   = get_the_excerpt();
                        $permalink = get_the_permalink();
                        echo '<h4>' . esc_attr( $title ) . '</h4>';
                        echo '<p>' . esc_attr( $excerpt ) . '</p>';
                        echo '<div><a class="button" href="' . esc_url( $permalink ) . '">View</a></div>';
                        ?>
                    </div>
                    <div class="col-md-6 d-flex align-items-center">
                        <?php the_post_thumbnail(); ?>
                    </div>

                </div>
            </div>

        <?php
        endwhile;

        wp_reset_postdata();
        ?>
    </div>
<?php }
?>

I've run into something very odd that I don't think I've ever seen before. I'm trying to query a custom-post-type and organize the results into groups by a custom taxonomy term. The post is Project and they're organized by a taxonomy of State.

The problem is that all of the States are printing out, but some of them don't display any associated Projects (even thought they have some, switching hide_empty between true and false produces expected results). I've tried a few combinations of term names and it appears that this only happens when the taxonomy term has a two-word name: e.g. "North Dakota". If I change the State on a Project to a different (single-named) State, the data does show.

Normally this looks something like:

State 1

  • Project
  • Project
  • Project

State 2

  • Project
  • Project

But some of the states are only displaying their name (taxonomy term name) and no Projects underneath.

State 3

State 4

I noticed the slugs of the two-named states had been concatenated into one word (northdakota), so I'm not sure if that could somehow be causing this. I tried reverting to north-dakota with no luck. Is there somewhere these exist in the db after you delete them that's maybe conflicting? Terms don't seem to have a Trash page.

This is the first project I've built using Gutenberg and ACF Gutenblocks but I don't see how that could be causing this, especially since all the data I'm querying is part of Core for this particular block.

Am I just missing something, or is this some sort of bug?

<?php
$states = get_terms( array(
    'taxonomy'   => 'state',
    'hide_empty' => true,
) );
foreach ( $states as $state ) {
    $state_name = $state->name;
    $state_slug = $state->slug;
    ?>
    <div class="state <?php echo esc_attr( $state_slug ); ?> mix">
        <h3 class="state-heading"><?php echo esc_attr( $state_name ); ?></h3>
        <?php
        $args = array(
            'post_type'      => 'project',
            'posts_per_page' => - 1,
            'orderby'        => 'title',
            'order'          => 'ASC',
            'tax_query'      => array(
                array(
                    'taxonomy' => 'state',
                    'field'    => 'name',
                    'terms'    => $state_slug,
                ),
            ),
        );
        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <div class="project">
                <div class="row">

                    <div class="col-md-6 d-flex flex-column justify-content-center">
                        <?php
                        $title     = get_the_title();
                        $excerpt   = get_the_excerpt();
                        $permalink = get_the_permalink();
                        echo '<h4>' . esc_attr( $title ) . '</h4>';
                        echo '<p>' . esc_attr( $excerpt ) . '</p>';
                        echo '<div><a class="button" href="' . esc_url( $permalink ) . '">View</a></div>';
                        ?>
                    </div>
                    <div class="col-md-6 d-flex align-items-center">
                        <?php the_post_thumbnail(); ?>
                    </div>

                </div>
            </div>

        <?php
        endwhile;

        wp_reset_postdata();
        ?>
    </div>
<?php }
?>
Share Improve this question asked Sep 3, 2019 at 19:12 zforszfors 17911 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is with your tax_query — the field (database column name) does not match the terms (database column values):

$args = array(
    ...
    'tax_query'      => array(
        array(
            'taxonomy' => 'state',
            'field'    => 'name',      // <- this should be set to 'slug'
            'terms'    => $state_slug, // because you provided term slug
        ),
    ),
);

I.e. If you provide term slugs (e.g. foo-bar), then the field should be slug; if you provide term names (e.g. Foo Bar), then the field should be name.

本文标签: Some posts not displaying by taxonomy term