php - Construct WP_Query args for multiple taxonomies and sort by terms

I'm trying to create a list of related posts from a couple of custom taxonomies and then sort them starting with on

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
  • The issue might be that parent terms are included in your taxonomy arrays… Try changing if ($cterm->slug != "work" to if ($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 using post__not_in when possible. In your case, you might find Tom's suggestion helpful. – Sally CJ Commented Mar 24 at 12:13
  • Thanks for the reply, Sally. I should have been clearer – those terms that I'm excluding with for example $cterm->slug != "work" are the parents. Both $catsArray and $areasArray have exactly what I want in them – the current posts terms without the parents. According to everything I've read, my code should be working but it's not. For the sorting, I want to sort the posts in PHP after the query. How do I prioritise the ones that match terms in $catsArray over the ones matching $areasArray? – tgerard Commented Mar 24 at 23:25
  • So, just to confirm - you’re fine with top-level terms (parent = 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 add var_dump( $rel_query->request ); after the new WP_Query call and share the output. – Sally CJ Commented Mar 25 at 0:25
  • Embarrassingly, the query is working now, using the code as posted above. I suspect that the returned results were so similar that I thought they were the same (it often returned the nine most recent posts). Many apologies for wasting your time on that. I still have the sorting issue. Just to be clear, I'm after the posts that match $catsArray to be the first. Often there will be more than nine of them, so that's all we'll have. If there are fewer than nine, I need posts that match $areasArray to make up the difference. Thanks again for your help. – tgerard Commented Mar 25 at 0:35
  • You're welcome! I was actually about to ask, "Are you sure the query isn't working?"

    发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744344282a4569588.html

相关推荐

  • php - Construct WP_Query args for multiple taxonomies and sort by terms

    I'm trying to create a list of related posts from a couple of custom taxonomies and then sort them starting with on

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信