wp query - Displaying Pages in Nav Sub-Menu with Specific Taxonomy Terms?

admin2025-06-04  0

I am trying to see if there is a way to have the sub-menu items change depending the taxonomy of the of the page that is in the menu? I currently use the following WP_Query I use to grab the top three items from the list of "Product Release" pages (these pages contain both release and solutions terms) associated with a "Solution" page and list them on the "Solution":

$my_terms = wp_get_post_terms(get_the_ID(), 'solutions', ['fields' => 'ids']);
$my_posts = new WP_Query(
    [
        'post_type' => 'page',
        'posts_per_page' => 3,
        'tax_query' => [
            'relation' => 'AND',
            [
                'taxonomy' => 'solutions',
                'field' => 'term_id',
                'terms' => $my_terms
            ],
            [
                'taxonomy' => 'release',
                'field' => 'term_id',
                'terms' => get_terms('release', ['fields' => 'ids'])
            ]
        ],
        'order'          => 'DESC',
        'orderby'        => 'title',
    ]
);

However, I need to also be able to do something similar in the menu. Is there a way that pages in the menu, with only a solutions term set, can have something like the above run on it to generate a submenu consisting of the top three pages that have both solutions and release terms?

The theme I am using is a modified twentyseventeen theme and the nav menu is the core functionality included with twentyseventeen.

I am trying to see if there is a way to have the sub-menu items change depending the taxonomy of the of the page that is in the menu? I currently use the following WP_Query I use to grab the top three items from the list of "Product Release" pages (these pages contain both release and solutions terms) associated with a "Solution" page and list them on the "Solution":

$my_terms = wp_get_post_terms(get_the_ID(), 'solutions', ['fields' => 'ids']);
$my_posts = new WP_Query(
    [
        'post_type' => 'page',
        'posts_per_page' => 3,
        'tax_query' => [
            'relation' => 'AND',
            [
                'taxonomy' => 'solutions',
                'field' => 'term_id',
                'terms' => $my_terms
            ],
            [
                'taxonomy' => 'release',
                'field' => 'term_id',
                'terms' => get_terms('release', ['fields' => 'ids'])
            ]
        ],
        'order'          => 'DESC',
        'orderby'        => 'title',
    ]
);

However, I need to also be able to do something similar in the menu. Is there a way that pages in the menu, with only a solutions term set, can have something like the above run on it to generate a submenu consisting of the top three pages that have both solutions and release terms?

The theme I am using is a modified twentyseventeen theme and the nav menu is the core functionality included with twentyseventeen.

Share Improve this question asked Aug 18, 2018 at 4:18 OpensaurusRexOpensaurusRex 16312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to find a solution by creating a class that adds a filter that runs a meta query and an extra checkbox to the menu editor for the root items that need to be generated.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749048232a315912.html

最新回复(0)