wp_get_nav_menu_items doesn’t return custom post type item


I try to achieve a menu like this (this is the actual menu).

enter image description here

There is a tree of projet coordinated by the wordpress menu that manage route and navigation.

Here is a scheme of the menu and the corresponding route.

 |__ PAINT        (SITE_URL/paint)
 |    |__ post_1  (SITE_URL/paint/post_1)
 |__ VIDEO        (SITE_URL/video)
      |__ post_2  (SITE_URL/video/post_2)
      |__ post_3  (SITE_URL/video/post_2)

First of all, know that post_1, post_2 and post_3 are custom post type generated here by CPT UI.

I need to get this menu in Js and in php, so I created a class in order to parse the menu returned by wp_get_nav_menu_items. You will find the class FP_Nav inside fp_nav.php this git gist.

As the route doesn’t really exist while it’s set in the wordpress menu as Custom link (ie. /paint, /video in the below scheme). I use an action do_parse_request in order to prevent the native parse_request if the url match some of mine in my menu and let the parse_request execute if not. (find my custom “router” in fp_router.php also in the previous git gist

Here is my functions.php (in the same git gist) in order to know the order of how it’s executed.

The problem is:

When I load my class FP_Nav that is supposed to get all the item in the menu using wp_get_nav_menu_items and that should be run before the headers are sent (because I need to parse the request using the menu items), this function wp_get_nav_menu_items does not return any of my custom post that is set in the menu.

I declare my CPT in the action init but I suspect that my class FP_Nav is loaded before the CPT is loaded because in the index.php (template file), the menu got every fields and if I declare the CPT outside the init action as well.

Also I have to load this class BEFORE the headers are sent so I can move out the CPT declaration outstide the init action but I’m not sure it’s a good idea and somehow I would like to produce good code so if there is another solution, I’m on!

, , Liroo Pierre 4 years 2019-10-24T03:10:33-05:00 0 Answers 132 views 0

Leave an answer