4

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="small-6 columns">
        <div class="img-box-shadow">
            <a href="<?php the_permalink(); ?>">
                <?php echo the_post_thumbnail(); ?>
            </a>
        </div>
    </div>
    <div class="small-6 columns">
        <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

Pieter Goosen
  • 9,768
  • 5
  • 35
  • 55
wharfdale
  • 1,148
  • 6
  • 23
  • 53
  • Any solutions here? I've seen many others with similar questions and also no responses. – wharfdale Dec 30 '14 at 15:08
  • For future reference, you may want to tag your question properly, many wordpress experts here might not have seen your question since you haven't tagged it [tag:wordpress]. Also, if you don't get an answer soon, you can flag your own question and ask for it to be migrated to [wordpress.se] (which is a site dedicated to WordPress), you may get better answers there. – Madara's Ghost Dec 31 '14 at 12:56
  • Heavily updated my code with a change, hopefully closer to what I am trying to achieve/explain. – wharfdale Dec 31 '14 at 13:07
  • What's the output of `var_dump( $category );` ? Any sticky posts? Why are you using `'numberposts' => -1` when you already set the `posts_per_page` parameter to one ? – birgire Jan 01 '15 at 09:33
  • The var_dump output is NULL. – wharfdale Jan 03 '15 at 00:10
  • None of what I have above is working really, even if I remove category__in it just shows the same post across all categories right now, seems I need a major change to the code. – wharfdale Jan 03 '15 at 00:12

2 Answers2

2

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'
                ]
        ]
];
Pieter Goosen
  • 9,768
  • 5
  • 35
  • 55
  • I think I've got it! Here is a pastebin of the small change to the arguments which seem to have fixed all. Alongside your code too. http://pastebin.com/Fc7KTUB5. Will reply again shortly when confirmed fully. – wharfdale Jan 04 '15 at 15:23
  • Great, holding thumbs for you. My syntax should work, should not be necessary for a full meta query. But again, it is 6 of one and half dozen of the other :-) – Pieter Goosen Jan 04 '15 at 16:45
  • My pleasure. Enjoy :-) – Pieter Goosen Jan 04 '15 at 17:36
0

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();
AndrewVT
  • 335
  • 1
  • 8
  • My get_field is bringing back nothing, NULL. Even if I remove 'category__in' it somehow and from somewhere, just brings the same post in across all categories. I've updated my post to show a live link. I firstly need to get the value in the get_field to work. – wharfdale Jan 03 '15 at 13:32
  • What happens if you hardcode your expected category: 'category__in' => 'my_category'? That will at least tell you if that part of your code is working as expected. – AndrewVT Jan 03 '15 at 17:37
  • Ok 2 things. If I edit any post, and don't even select a value for the 'feature in category' dropdown, it will appear at the top where this query is, it shouldn't as I haven't defined a 'featured in category'. Also, the same post appears in the top of every category. Here is a live URL: http://highways.designlocker.co.uk/category/health-safety/ – wharfdale Jan 03 '15 at 18:17