0

Ok found this code here by PRESSWORD

add_filter( 'wp_get_nav_menu_items', 'cpt_locations_filter', 10, 3 );

function cpt_locations_filter( $items, $menu, $args ) {
  $child_items = array(); 
  $menu_order = count($items); 
  $parent_item_id = 0;

  foreach ( $items as $item ) {
    if ( in_array('locations-menu', $item->classes) ){ //add this class to your menu item
        $parent_item_id = $item->ID;
    }
  }

  if($parent_item_id > 0){

  foreach ( get_posts( 'post_type=cpt-post-type-here&numberposts=-1' ) as $post ) {
    $post->menu_item_parent = $parent_item_id;
    $post->post_type = 'nav_menu_item';
    $post->object = 'custom';
    $post->type = 'custom';
    $post->menu_order = ++$menu_order;
    $post->title = $post->post_title;
    $post->url = get_permalink( $post->ID );
    array_push($child_items, $post);
  }

  }

      return array_merge( $items, $child_items );
    }

It works well, except I want the new posts to be added to the bottom. Right now the newest post is at the top, and I want to reverse that order. Also, when I go to just drag the order in the menu editor I get an error 'The given object ID is not that of a menu item.' is there something missing in this code?

2 Answers 2

0

This code is pulling in custom posts automatically and setting menu order as it does within the function, so trying to change the order in the menu editor will likely cause an error.

To change the order change this line:

 foreach ( get_posts( 'post_type=cpt-post-type-here&numberposts=-1' ) as $post ) {

to

 foreach ( get_posts( 'post_type=cpt-post-type-here&numberposts=-1&order=ASC' ) as $post ) {

I've added an 'order' statement to sort ASC.

I prefer arrays because they are easier for me to read. You could rewrite your query to this:

$args = array(
    'post_type'=> 'your-custom-post-type',
    'numberposts' => -1,
    'order'    => 'ASC'
);

foreach ( get_posts( $args ) as $post ) {
0

To reverse the order of the new posts and resolve the issue with dragging in the menu editor, you can modify the code as follows:

add_filter( 'wp_get_nav_menu_items', 'cpt_locations_filter', 10, 3 );

function cpt_locations_filter( $items, $menu, $args ) {
    $child_items = array();
    $menu_order  = count( $items );
    $parent_item_id = 0;

    foreach ( $items as $item ) {
        if ( in_array( 'locations-menu', $item->classes ) ) {
            // add this class to your menu item
            $parent_item_id = $item->ID;
        }
    }

    if ( $parent_item_id > 0 ) {
        // Fetch posts in descending order
        $posts = get_posts( array(
            'post_type'      => 'cpt-post-type-here',
            'numberposts'    => -1,
            'order'          => 'DESC', // Set the order to DESC
            'orderby'        => 'date', // You can change this to a different field if needed
        ) );

        foreach ( $posts as $post ) {
            $post->menu_item_parent = $parent_item_id;
            $post->post_type        = 'nav_menu_item';
            $post->object           = 'custom';
            $post->type             = 'custom';
            $post->menu_order       = ++$menu_order;
            $post->title            = $post->post_title;
            $post->url              = get_permalink( $post->ID );
            array_push( $child_items, $post );
        }
    }

    return array_merge( $items, $child_items );
}

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.