I'm trying to create a list of related posts from a couple of custom taxonomies and then sort them starting with one taxonomy's terms.
The post type is "work" and the two taxonomies are "work_categories" and "work_areas". Both the taxonomies are hierarchical and I need to ignore the parent level and only use the child terms.
My first step has been to create arrays for each of the custom taxonomies, leaving out the parents:
<?php
$catsArray = array();
$cterms = get_the_terms( $post->ID, 'work_categories' );
foreach($cterms as $cterm) : ?>
<?php if ($cterm->slug != "work" && $cterm->slug != "news-events" && $cterm->slug != "featured-news" && $cterm->slug != "featured-work") : ?>
<?php $catsArray[] = $cterm->term_id; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php
$areasArray = array();
$aterms = get_the_terms( $post->ID, 'work_areas' );
foreach($aterms as $aterm) : ?>
<?php if ($aterm->slug != "project-names" && $aterm->slug != "sector" && $aterm->slug != "state") : ?>
<?php $areasArray[] = $aterm->term_id; ?>
<?php endif; ?>
<?php endforeach; ?>
This is working fine, the arrays are just what I need.
I then write the args for the query:
<?php
global $post;
$rel_args = array(
'posts_per_page' => 9,
'order' => 'DESC',
'orderby' => 'ID',
'post_type' => 'work',
'post__not_in' => array( $post->ID ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'work_categories',
'field' => 'id',
'terms' => $catsArray,
'include_children' => false,
'operator' => 'IN'
),
array(
'taxonomy' => 'work_areas',
'field' => 'id',
'terms' => $areasArray,
'include_children' => false,
'operator' => 'IN'
)
),
);
$rel_query = new WP_Query( $rel_args );
?>
However, this seems to be returning all posts.
I'm unsure whether the 'field' should be 'id' or 'term_id', but I've tried both with no luck. I've also tried limiting the tax_query to only one taxonomy and even just one term, hoping to get a clue as to where the issue is, but I get all of the posts in each situation.
So my first question is why this isn't returning the desired posts, and my second is how I could sort these so that the posts that match "work_categories" come first and then those from "work_areas".
Thanks in advance. Hopefully this isn't a duplicate, I couldn't find anything despite a lot of trawling.
I'm trying to create a list of related posts from a couple of custom taxonomies and then sort them starting with one taxonomy's terms.
The post type is "work" and the two taxonomies are "work_categories" and "work_areas". Both the taxonomies are hierarchical and I need to ignore the parent level and only use the child terms.
My first step has been to create arrays for each of the custom taxonomies, leaving out the parents:
<?php
$catsArray = array();
$cterms = get_the_terms( $post->ID, 'work_categories' );
foreach($cterms as $cterm) : ?>
<?php if ($cterm->slug != "work" && $cterm->slug != "news-events" && $cterm->slug != "featured-news" && $cterm->slug != "featured-work") : ?>
<?php $catsArray[] = $cterm->term_id; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php
$areasArray = array();
$aterms = get_the_terms( $post->ID, 'work_areas' );
foreach($aterms as $aterm) : ?>
<?php if ($aterm->slug != "project-names" && $aterm->slug != "sector" && $aterm->slug != "state") : ?>
<?php $areasArray[] = $aterm->term_id; ?>
<?php endif; ?>
<?php endforeach; ?>
This is working fine, the arrays are just what I need.
I then write the args for the query:
<?php
global $post;
$rel_args = array(
'posts_per_page' => 9,
'order' => 'DESC',
'orderby' => 'ID',
'post_type' => 'work',
'post__not_in' => array( $post->ID ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'work_categories',
'field' => 'id',
'terms' => $catsArray,
'include_children' => false,
'operator' => 'IN'
),
array(
'taxonomy' => 'work_areas',
'field' => 'id',
'terms' => $areasArray,
'include_children' => false,
'operator' => 'IN'
)
),
);
$rel_query = new WP_Query( $rel_args );
?>
However, this seems to be returning all posts.
I'm unsure whether the 'field' should be 'id' or 'term_id', but I've tried both with no luck. I've also tried limiting the tax_query to only one taxonomy and even just one term, hoping to get a clue as to where the issue is, but I get all of the posts in each situation.
So my first question is why this isn't returning the desired posts, and my second is how I could sort these so that the posts that match "work_categories" come first and then those from "work_areas".
Thanks in advance. Hopefully this isn't a duplicate, I couldn't find anything despite a lot of trawling.
Share Improve this question asked Mar 24 at 7:18 tgerardtgerard 1033 bronze badges 9
if ($cterm->slug != "work"
toif ($cterm->parent && $cterm->slug != "work"
and see if that helps. (Apply the same parent check to$aterms
) Regarding your second question, it depends on whether you want to sort at the database level or in PHP after querying the posts. Sorting in PHP is an option if you don’t need database-level ordering. Also, avoid usingpost__not_in
when possible. In your case, you might find Tom's suggestion helpful. – Sally CJ Commented Mar 24 at 12:13parent = 0
) being included in those arrays? And let’s set sorting aside for now and focus on your query. Try adding'suppress_filters' => true
to your query args and see if that returns the expected posts. If it does, remove that arg and then addvar_dump( $rel_query->request );
after thenew WP_Query
call and share the output. – Sally CJ Commented Mar 25 at 0:25发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744344282a4569588.html