I am working on a WordPress project where I want to dynamically filter menu items (and possibly their parent items) based on whether they are associated with any published posts. Specifically, I am looking to dynamically filter out categories from the menu if they have no association to posts with a "published" state. Furthermore, if a parent category has no children with published posts, that parent category should be dynamically filtered out from the menu as well.

The updating of the Wordpress navigation block should happen whenever a post is updated.

1 - I have been using PHP to query the database and determine whether a category has any published posts associated with it. I created a function to check each category and its parent category to see if there are any published posts and dynamically adjust the menu accordingly.

Here is a snippet of the PHP code I am working with to detect if a category has any published post associated with it, the basic model is :

function has_published_posts($term_id) {
    global $wpdb;
    $post_count = $wpdb->get_var($wpdb->prepare("
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts p
        JOIN {$wpdb->prefix}term_relationships tr ON p.ID = tr.object_id
        JOIN {$wpdb->prefix}term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        WHERE tt.term_id = %d
        AND tt.taxonomy = 'product_cat'
        AND p.post_type = 'product'
        AND p.post_status = 'publish'", 

    return $post_count > 0;

So far I am able to detect if a child-category or parent-category has any published post associated with it. This part is achieved.

2- However, I am currently searching how to hook into the new WordPress Navigation Block API to perform this filtering. I have been trying to find a relevant hook to access the Navigation Block API.

Does anyone know of a hook or method within the Navigation Block API that can be used to dynamically filter menu items ?

I have seen there is Github and Wordpress pages that could help :

If someone has some experience with filtering out the new Navigation Block, thanks for sharing your experience.

Any guidance on this matter would be greatly appreciated. Thank you!

  • Would it be correct to rephrase the original goal as "I only want to show links to categories that have posts in my menu"?
    – Tom J Nowell
    Commented Sep 22, 2023 at 17:32
  • Thanks for your comment. Yes it is this, more precisely "I only want to show links to categories that have posts in 'publish' state in my menu", I correct right now, thanks. Do you have any idea by the way ?
    – DiaJos
    Commented Sep 22, 2023 at 17:33
  • Note that there have been a few attempts by core to figure out navigation blocks, and it would help to set some constraints, e.g. I assume you mean to do this only on the frontend in the rendered block? And that the site editor/block editor/REST API aren't a part of your question? I also assume that dynamically inserting categories is not an option? Have you tried the filters used for the classic wp_nav_menu function?
    – Tom J Nowell
    Commented Sep 22, 2023 at 17:35
  • I see, In fact I am interested in any solution that would end up in displaying my menu in a up to date state. The simplest the better. I will give an another shot to wp_nav_menu. Thanks
    – DiaJos
    Commented Sep 22, 2023 at 17:45

1 Answer 1


I finally used the classic wp_nav_menu interface, it seems the last Navigation Bloc is great for ease of use, but for customization it seems to be less user friendly from an API/hook point of view.

I set this topic as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.