Support

Account

Home Forums ACF PRO Unable to query custom post type with specific category

Solved

Unable to query custom post type with specific category

  • Hi,

    Based on a specific category, I have trouble querying the posts from the Custom Post Type I created in ACF. However, if I remove 'category_name' => 'featured',, the posts will show again. Still, the category name I wished to list without a link won’t show.

    Below is my WP_Query code.


    <?php $featured_slides = new WP_Query(
    array(
    'post_type' => 'project',
    'posts_per_page => 10,
    'category_name' => 'featured',
    )
    ) ?>

    <main class="common_margin">
    <div class="hero_slide">
    <div class="hero_slide-swiper swiper_container">
    <?php if( $featured_slides->have_posts() ) : ?>
    <div class="hero_slide-container swiper-wrapper">
    <?php while( $featured_slides->have_posts() ) : $featured_slides->the_post(); ?>
    <div class="hero_slide-item swiper-slide">
    <div class="hero_slide-item--wrap">
    ">
    <?php $img = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
    if( has_post_thumbnail() ) : ?>
    " class="hero_slide-item--img" alt="<?php the_title(); ?>">
    <?php endif; ?>
    <div class="hero_slide-item--desc">
    <p>
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
    </p>
    <h4><?php the_title(); ?></h4>
    </div>

    </div>
    </div>
    <?php endwhile; ?>
    </div>
    <?php wp_reset_postdata(); endif; ?>
    <div class="swiper-pagination"></div>
    </div>
    </div>
    </main>

  • How is the category added to the post?

  • Hi John,

    Thanks for the reply.
    I am creating and adding the category the normal way, just like when we are in “Posts”. I have a screenshot follow-up below.

  • Did you copy the code exactly? Your original code has a typo in it, missing closing single quote around posts_per_page

    'posts_per_page => 10,

    Are you sure the category slug is “featured”

    I just did a test, correcting the typo and it is returning the correct posts for me.

  • My apologies.
    I checked my codes.
    There is a closing quote after ‘posts_per_page’.
    I think I mistyped it in the message box.
    And yes, the slug is “featured”.
    Please see the screenshot below.
    Thank you.

  • This reply has been marked as private.
  • Like I said, I tested it.

    I created a CPT “project” using ACF. I added the built in WP category taxonomy to it. I created a “project” and added the “featured” category to it. Then I did the same query you are doing.

    Everything worked as expected.

    Did you also create a custom taxonomy?

  • Yes, I created the custom taxonomy in ACF too.
    Below are the screenshots.
    So you can have a brief idea of what I have done.
    Thank you.

  • The “catetory_name” argument for WP_Query() will only work for build in WP category taxonomy. You must use a taxonomy_query for custom.

    
    'tax_query' => array(
      array(
        'taxonomy" => 'YOUR CUSTOM TAXONOMY NAME HERE',
        'field' => 'slug',
        'terms' => 'featured'
      }
    )
    
  • Hi John,

    Thanks for the guidance.
    It is working now.
    However, I still have a problem showing the category in the slide.
    Below is the code I use to call out the category.
    I don’t want the category to come with the link, and this is the code.

    <p>
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
    </p>
  • you can’t use get_the_category() either. All category functions in WP and anything that has “category” in it is for the built in WP category taxonomy.

    For custom taxonomies you have to use get_terms(). This will return an array of terms that you’ll need to loop over (event if there is only one term).

  • Thanks, John.
    If you don’t mind, I am going to ask you a dumb question, as I am still learning.
    So, a lot of time, I will google for answers related to what I want.
    Below is the solution I found after you told me to use “get_terms”.
    Instead of showing only the selected categories for the post, it listed every single category I have created. Can you give me some clue why?

    $args = array(
      "hide_empty" => false,
      "taxonomy" => "project-category",
    );
    
    $terms = get_terms($args);
    foreach($terms as $term){
      echo $term->name.", ";
    }?>

    I hope I am not disturbing you, though.

  • Sorry, that was my fault, pointed you at the wring thing.

    You need to use wp_get_post_terms() or get_the_terms().

  • No worries.
    Mistakes always happen.
    And thank you so much for being patient with me.
    Problem solved and case closed. 🙂

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

You must be logged in to reply to this topic.