Display post as featured if taxanomy matches current category

I have used ACF. I have added the 'taxonomy' select dropdown field to the post edit page. From the dropdown I select which category this post should be featured in, but my code is displaying the same post as featured across all categories.

Below is the code in the category.php file. I need it to display the most recent post which has been given a 'Feature In Category', and to therefore be featured in the category I have defined.

enter image description here

My current loop in category.php

<?php 

$category = get_field('feature_in_category');

// args
$args = array(
    'numberposts' => -1,
    'posts_per_page' => 1,
    'category__in' => $category,
    'orderby'=> 'modified'
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>


<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="infoflowing-style">
        <div class="infoflowing-style">
            <a href="<?php the_permalink(); ?>">
                <?php echo the_post_thumbnail(); ?>
            </a>
        </div>
    </div>
    <div class="infoflowing-style">
        <h3><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h3>
        <p><?php echo the_excerpt(); ?></p>
    </div>


<?php endwhile; ?>

<?php else : echo '<p style="color:#fff;">no posts</p>'; ?>

<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Pastebin: http://pastebin.com/NR3UanAd

Comments


  • Mark

    After reviewing the documentation here http://codex.wordpress.org/Class_Reference/WP_Query it appears that your code is doing all the right stuff. I believe your answer lies in the actual query string that is being used to produce your results. You should be able to see that query string in the properties of the query object by doing:

    <?php 
    
    $category = get_field('feature_in_category');
    
    // args
    $args = array(
        'numberposts' => -1,
        'posts_per_page' => 1,
        'category__in' => $category,
        'orderby'=> 'modified'
    );
    
    // get results
    $the_query = new WP_Query( $args );
    echo '<pre>'; print_r($the_query); die();
    

    I suggest running the query string once you see it directly on the database using something like phpMyAdmin or Navicat to connect directly to the database. You should be able to adjust your query there to produce your desired results and then adjust your arguments accordingly. It may also be that there is nothing wrong with your query and that the issue lies with the category functionality instead. This debugging method should reveal that as well if that is the case.

    UPDATE: I think you just need to use 'cat' and not 'category__in'. Also, make sure you use what you set for Field Name inside of the call to get_field. This feedback is based on this example: http://www.the-perfect-life.org/html-5-css-3-php-wordpress-jquery-javascript-photoshop-illustrator-tutorial/how-to-create-custom/wordpress-plugin-theme-codex-code-function/advanced-custom-fields-get-field-taxonomy-query-related-posts-by-tag-and-category/

    $categoryValue = get_field('feature_in_category');
    $args=array(
    'cat' => $categoryValue,
    'posts_per_page'=>1 // Number of related posts to display
    );
    $my_query = new wp_query( $args );
    while( $my_query->have_posts() ) {
    $my_query->the_post();
    ?>
    <div>
    <a href="<? the_permalink()?>">
    <?php the_title(); ?>
    </a>
    </div>
    wp_reset_query();
    
  • Bill

    I'm not very familiar with ACF and haven't yet used it. I had a look at some documentation etc, and this is what I've found:

    ACF TAXONOMY FIELD

    The ACF taxonomy field will return NULL in your application as the ID being passed to get_field is a category ID, which is a non valid post ID. Yes, you can pass a category ID to get_field, but then there must be a custom field assigned to that particular category in order to return the value of that custom field.

    Look at the ACF docs

    $field = get_field($field_name, $post_id, $format_value); 
    

    $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

    ACCESS STORED DATA BY ACF

    As I stated before, I'm not familiar with ACF but it seems that ACF field data is stored in the same way as data is stored by the default custom fields. So you will have a matching pair of key=>value sets. This data can be accessed and retrieved by a meta_query

    THE IDEA

    The ACF Taxonomy dropdown saves a selected value which translated into a category. Again, I'm not sure whether it saves the name, slug or ID, but from what I can pick up, it saves the category ID, so I will make my assumptions on that

    So, the first thing to do here is, get the currently viewed category's ID. This can can be retrieved by get_queried_object_id()

    Now that you have the current category ID, you will need to match that to a value for the specific meta_key=feature_in_category and return the posts that has this specififc key=>value pair attached to it

    THE CODE

    Your code should look something like this with the assumption ACF data is stored in the same way as data is stored from a custom field (This code requires PHP5.4+, for earlier versions, change [] to array())

    $cat_id = get_queried_object_id(); // Assumption that category ID is saved by ACF
    
    $args = [
        'posts_per_page' => 1,
        'orderby'        => 'modified',
        'meta_key'       => 'feature_in_category',
        'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
    ];
    $q = new WP_Query( $args );
    
    if( $q->have_posts() ) {
        while( $q->have_posts() ) {
            $q->the_post();
    
            // YOUR TEMPLATE TAGS AND MARKUP
    
        }
        wp_reset_postdata();
    }
    

    If the category ID is not saved by the ACF field, and it saves the name or slug, you can change the code as follows

    $category = get_queried_object()->name; // For category name
    

    or

    $category = get_queried_object()->slug; // For category slug
    

    Then in your query arguments, change

    'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
    

    to

    'meta_value' => $category, 
    

    EDIT

    In addition and posted as a comment, the query aruments as used by the OP, working.

    $cat_id = get_queried_object_id();
    $args = [
            'posts_per_page' => 1,
            'orderby'        => 'modified',
            'meta_query' => [
                    [                        
                            'key' => 'feature_in_category',
                            'value' => $cat_id,
                            'compare' => 'IN'
                    ]
            ]
    ];
    

Add Comment