Support

Account

Home Forums Backend Issues (wp-admin) WP_Query using ACF Taxonomy Category

Helping

WP_Query using ACF Taxonomy Category

  • I’m trying to create a WP_Query loop with a custom post type of project and using ACF Taxonomy field to filter just those categories. I found in this forum an example, but it’s not working.

    plus I think I need to add something for if there is only 1 category selected rather than multiple.

    The loop is working without the taxonomy arg. I think I’m pretty close, but nothing shows when I add in the taxonomy arg.

    here is what I have:

    
    // this field can return either a single category or multiple
    $categories = get_field('project_slider_category');
    if ($categories) {
      if (!is_array($categories)) {
        $categories = array($categories);
      }
    }
    
    $args = array(  
        'post_type' => 'project',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'tax_query' => array(
          array(
            'taxonomy' => 'categories',
            'terms' => $categories,
          ),
        ), 
    );
    
    $loop = new WP_Query( $args );
    

    I attached a screenshots with my ACF settings and showing what is selected in my example.

  • I really do not see anything seriously wrong, the only thing I can think of is that you either have the wrong taxonomy name or that the values your trying to use in the query are not integers

    
    // this field can return either a single category or multiple
    // ensure that you are only getting the ID values of the terms and not term objects
    $categories = get_field('project_slider_category', false, false);
    if ($categories) {
      if (!is_array($categories)) {
        $categories = array($categories);
      }
      // make sure all values are integers
      $categories = array_map('intval', $categories);
    }
    
    $args = array(  
        'post_type' => 'project',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'tax_query' => array(
          array(
            // make sure the category name is "categories"
            'taxonomy' => 'categories',
            'terms' => $categories
          ),
        ), 
    );
    
    $loop = new WP_Query( $args );
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.