Support

Account

Home Forums General Issues Taxonomy Field in Post Query?

Solving

Taxonomy Field in Post Query?

  • I’ve created a custom template for an archive page for a custom post type. Currently, I have it set up to only display posts that the current logged in user is the author of. I would also like to add the ability to filter by category.

    This is my code so far:

    
    <div id="lorebook-archives">
        <?php // Only Show Current User's Posts
          if ( is_user_logged_in() ):
            global $current_user;
        wp_get_current_user();
    
        $author_query = array(
          'posts_per_page' => '-1',
          'author' => $current_user->ID,
          'post_type' => 'location',
          'category_name' => **ACF FIELD GOES HERE**
        );
    
        $author_posts = new WP_Query($author_query);
        while($author_posts->have_posts()) : $author_posts->the_post();
        ?>
    
          <?php
          $bg = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' );
          ?>
    
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
              <div id="archive-post" style="background: url('<?php echo $bg[0]; ?>') repeat center center #fbfbfb; background-size:cover;">
                <h1><?php the_title(); ?></h1>
              </div>
            </a>
    
        <?php
        endwhile;
      endif; ?>
    
      </div>
    

    You can see where my query array says “category_name”. That is where I would like to use the data from an ACF Taxonomy field. If somebody selects “Category A” from an ACF form on the page, I’d like my code to be updated to ‘category_name’ => ‘category-a’.

    I don’t know how to do this though.

  • Yeah I couldn’t quite get that to work. I did play around some more with meta_query and I’m almost there. This is my current code:

    
    <div id="lorebook-archives">
        <?php // Only Show Current User's Posts
          if ( is_user_logged_in() ):
            global $current_user;
        wp_get_current_user();
    
        $universe = get_field('universe-selector');
    
        $author_query = array(
          'posts_per_page' => '-1',
          'author' => $current_user->ID,
          'post_type' => 'location',
          'meta_query' => array(
            array(
              'key' => 'location-universe',
              'value' => $universe
            )
          )
        );
    
        $author_posts = new WP_Query($author_query);
        while($author_posts->have_posts()) : $author_posts->the_post();
        ?>
    
          <?php
          $bg = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' );
          ?>
    
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
              <div id="archive-post" style="background: url('<?php echo $bg[0]; ?>') repeat center center #fbfbfb; background-size:cover;">
                <h1><?php the_title(); ?></h1>
              </div>
            </a>
    
        <?php
        endwhile;
      endif; ?>
    
      </div>
    

    I have a Post Object custom field on my page called “universe-selector”. I’m using this as a variable for the value of the “location-universe” field on the individual posts.

    I can’t get this to work with a Post Object field, though. I DID get it to work with a Relationship Field, by setting the Return Format to Post ID. But that option doesn’t seem to be available for Post Object fields, at least not in the free ACF v4. Is there any way I can get the Post ID from the Post Object field without requiring ACF Pro?

  • Sorry about the late reply. You should be able to get it from the post object returned. For example

    
    $post_object = get_field('post_object');
    $id = $post_object->ID;
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Taxonomy Field in Post Query?’ is closed to new replies.