Advanced Custom Fields get field taxonomy. Query related posts by tag and category

This article was written by in October 5, 2013, & may not be posted on other sites!
Original source url for this article: Advanced Custom Fields get field taxonomy. Query related posts by tag and category

How to Query related posts by specific tag and / or category using Advanced Custom Fields get field taxonomy.
Code example below shows you how to use ACF – Advanced Custom Fields plugin in WordPress. How to add 2 taxonomy > select dropdown menus to each post. One for categories and one for tags. In your posts you can then select a specific tag and / or category and in the single.php query related posts only by selected tag and category. This will work even if the post has multiple other categories or tags assigned to it through the default Category or Tags panels on the right when editing a post. Full code below.

 

Install the ACF – Advanced Custom Fields Plugin

  1. Go to Plugins > Add new and search for plugin “Advanced Custom Fields”.
  2. Click “install” to install the Advanced Custom Fields plugin, and then activate it.

 

Add 2 taxonomy > select dropdown (by tag and category) to every post with Advanced Custom Fields plugin

Advanced Custom Fields get field taxonomy. Query related posts by tag and category tutorial

  1. Go to Custom fields > Custom fields.
  2. Add a new fieldgroup and name it, for example “Posts”.
  3. Under Location, Choose : Show this field group if > Post type > is equal to > post. This means that all the extra fields you add in this fieldgroup “Posts”, will show on all posts.
  4. Click +Add field – button to add the taxonomy > select dropdown.
  5. Fill in the fields as follows:
    Field label: The name of this field is seen as a small header above the field inside the post.
    Field name: This is the name called from within your template / single.php to get the selected value.
    Field type: select “Taxonomy”
    Field instructions: The text in this field is seen as a small helptext inside the post.
    Required? : Well, set this to “no” to begin with
    Taxonomy: Select “Categories”.
    Field type: set this to “Select” to get the dropdown menu.
    Allow Null? : Set this to “yes” to begin with, meaning that you will have the option “none” in the taxonomy dropdown in addition to all the categories.
    Return value: Set this to “Term ID”, this will return the ID number of the category.
  6. Do step 4-5 to create another “select taxonomy dropdowm menu” for tags. make sure to select Taxonomy: “Tags” for this one.
  7. Save or update your new fieldgroup for all posts.
  8. Those 2 extra “select taxonomy dropdown menus” are now seen inside all posts. Simpy edit or add a new post, and select a category and / or tag value from your new taxonomy dropdown menus. If the dropdowns areempty you need to add som categories and tags to your posts (since the dropdown is showing existing tags or categories). You can add as many categories and tags to a post as you wish in the default Tags / Categories panels to the right. This will not interfere with the dropdown functionality.

Advanced Custom Fields get field taxonomy. Query related posts by tag and category

 

How to query_posts by selected Category and / or Tag to create a “related posts” section on all single post pages.

Ok, we have everything set up by now. Create some posts with one or several different categories and tags. Choose a tag from the new “Select Taxonomy Dropdown”. Save or update post.

In this example we are working with the single.php file to list related posts by categiry and / or tag only on single post pages. But you can add this functionality to any template or file in your theme. Always work with a custom theme or childtheme in WordPress! Ok here we go:

  1. Open your single.php file in a text editor or inside wordpress admin > Appearance > editor.
  2. Add a <div> or a wrapper to hold your “related posts” content and place it wherever you want inside the loop, meaning after <?php while ( have_posts() ) : the_post(); ?> but before <?php endwhile; // end of the loop. ?>
  3. All following code should go inside this <div> or wrapper.
  4. To put the ID number of your selected tag inside a variable, use code:
    $categoryValue = get_field('special_category');
    ‘special_category’ must match the “Field name” you wrote in your field group.
    To put the ID number of your selected tag inside a variable, use code:
    $tagValue = get_field('special_tag');
    ‘special_tag’ must match the “Field name” you wrote in your field group.
  5. To query_posts from selected category and tag and print out related posts as links, use this code below. A new query like this is not interfering with the main loop. This code will query posts by selected category and tag:
    <?php
    $tagValue = get_field('special_tag');
    $categoryValue = get_field('special_category');
    $args=array(
    'tag_id' => $tagValue,
    'cat' => $categoryValue,
    'posts_per_page'=>5 // 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();

 

Full code for the single.php to query related posts by specific category and / or tag.

Code below is complete. This code was used in a childtheme to Twenty Twelve, and will query posts by selected category, tag, category and tag, or list related posts from the posts regular tags from the right panel if nothing is selected in the dropdown menus. The related posts are shown as Title + thumbnail image (featured image). Just copy and paste this code to create a custom single.php

<?php
/**
* The Template for displaying all single posts.
* Author: Olivia Hoback
* www.olivia.nu
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/

get_header(); ?>

<div id="primary" class="site-content">
<div id="content" role="main">

<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>


<div id="related-posts">
<h1>Related posts</h1>
<?php

//get te selected values and put them in variables
$tagValue = get_field('special_tag');
$categoryValue = get_field('special_category');


if($tagValue){ //if special tag is selected
if($categoryValue){ //if a special category is also selected

$args=array(
'tag_id' => $tagValue,
'cat' => $categoryValue,
'post__not_in' => array($post->ID),
'posts_per_page'=>5 // Number of related posts to display
);

$my_query = new wp_query( $args );

while( $my_query->have_posts() ) {
$my_query->the_post();
?>

<div class="related-post">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>

<?php
}
wp_reset_query();

}else{ //if only a tag is selected

$args=array(
'tag_id' => $tagValue,
'post__not_in' => array($post->ID),
'posts_per_page'=>5 // Number of related posts to display
);

$my_query = new wp_query( $args );

while( $my_query->have_posts() ) {
$my_query->the_post();
?>

<div class="related-post">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>

<?php }
wp_reset_query();
}


}
elseif($categoryValue){ //if only a category is selected


$args=array(
'cat' => $categoryValue,
'post__not_in' => array($post->ID),
'posts_per_page'=>5 // Number of related posts to display
);

$my_query = new wp_query( $args );

while( $my_query->have_posts() ) {
$my_query->the_post();
?>

<div class="related-post">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>

<?php }

wp_reset_query();


}else{ //if nothing is selected we will query related posts from from the regular tags in the right panel


$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);

if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts to display
'caller_get_posts'=>1
);

$my_query = new wp_query( $args );

while( $my_query->have_posts() ) {
$my_query->the_post();
?>

<div class="related-post">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>

<?}
}
$post = $orig_post;
wp_reset_query();
}


?>
</div><!-- #related-posts -->



<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>


</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *