• Ik heb een metabox gemaakt voor custom posts. De bedoeling een link te maken van een child naar een parent. Op zich werkt het goed maar in m’n keuze dropdown krijg ik ALLE parents te zien; en dat kan een beetje veel worden. Ik wil alleen die parents die nog geen child hebben. Hoe filter is dat?
    Dit is het relevante deel van de code:

    /**
     * The complete three part code for setting up the relationships
     */
    // FIRST PART
    add_action('admin_menu', function() { 
       remove_meta_box('pageparentdiv', 'story_frame', 'normal');
    });
    
    // SECOND PART
    // Here we create the meta box to be displayed on post editor screen
    add_action('add_meta_boxes', function() { 
       add_meta_box('story_frame-parent',               // unique ID
                    'Add parent story:',                // Title of the meta box
                    'story_frame_attributes_meta_box',  // Callback to display the ouput 
                    'story_frame',                      // Screen default = current
                    'side',                             // Where the box shows up
                    'high'                              // Core, high, low
                    //$callback_args                    // Arguments array for callback
                    );  
    });
      
    // THIRD PART
    // The callback function 
    function story_frame_attributes_meta_box($post) { 
    
      // $post is the current post of type: story_frame
      // $post_type_object tell's everything about post_type: story_frame
      $post_type_object = get_post_type_object($post->post_type);
    
      if ( $post_type_object->hierarchical ) {
    
        // Produces the dropdown list of pages to choose from
        $pages = wp_dropdown_pages( array (
                    'post_type'         => 'jhk_story', 
                    'selected'          => $post->post_parent,
                    'name'              => 'parent_id', 
                    'show_option_none'  => __('(no parent)'),
                    'sort_column'       => 'menu_order, post_title', 
                    'echo'              => 0
                    ));
    
        if ( ! empty($pages) ) { echo $pages; } 
    
      }
    
    } // story_frame_attributes_meta_box()
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • Try adding 'depth' => 0 to your arguments and see if that helps. Use this documentation guide for help: https://developer.wordpress.org/reference/functions/wp_dropdown_pages/.

    Thread Starter pagemaecker

    (@pagemaecker)

    Daniel,
    Dank voor de reactie, maar het beperken van de depth (het aantal posts) is geen garantie dat ik de posts die ik WEL wil hebben dan ook krijg.

    Moderator bcworkz

    (@bcworkz)

    You can use the ‘get_pages’ filter to remove undesirable pages from the passed array. You should be able to discern childless pages by lack of associated children in the passed array.

    Thread Starter pagemaecker

    (@pagemaecker)

    @bcworkz,
    That’s worth investigating!
    But where in my code do I add the filter?
    After: &pages=wp_dropdown_pages(…
    Before: if ( !empty( …
    Maybe?

    Thread Starter pagemaecker

    (@pagemaecker)

    Something like this maybe?

    add_filter( 'get_pages', 'my-filter-function' );
    
    function my-filter-function( $pages ) {
    
       Do your filtering here
    
       return $pages;
    }
    • This reply was modified 5 years, 11 months ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    Yes, that’s the idea. The challenge with these sort of filter hooks is only applying your adjustments where needed and not for other code using the same function. Sometimes there is something unique your code can check for in the data available. The other way to manage application is to not add your filter callback until just before it is needed, then remove it right after it’s used.

    The latter should work nicely for you. Add the filter just before calling wp_dropdown_pages(), then call remove_filter() right afterwards.

    I took a look at the page organization passed through this filter. I was hoping children would somehow be in their own sub-array below the parent, making determination of children simple. No such luck. It’s a flat indexed array. Fortunately, the default ordering seems to put children immediately after their parent. So determining childlessness is a matter of checking if the post_parent value of the next page in the array matches the ID of the current page.

    TIL using hyphens in PHP function names causes a syntax error. That’s why everyone uses underscores to separate words.

    Thread Starter pagemaecker

    (@pagemaecker)

    Well it took me about three days to crack this one. Yeh, I know… In my defense I can say that it’s my first WP project. I did it this way:

        $exclude = get_excluded_parents();
    
        $parents = get_posts(
            array(
                'post_type'   => 'jhk_story', 
                'orderby'     => 'ID', 
                'order'       => 'ASC', 
                'numberposts' => -1,
                'exclude'     => $exclude,
            )
        );
    
       And for the function:
    
    /* 
     * Filters excluded parents from post_type: story_frame
     */
    function  get_excluded_parents() {
    
      // Select all posts with post_type = story_frame
      $childs = get_posts (
        array (
          'post_type'   => 'story_frame',
          'orderby'     => 'ID',
          'order'       => 'ASC',
          'numberposts' => -1
        )
      );
    
      $exclude = array();
    
      foreach ( $childs as $child ) {
    
        if ( 0 < $child->post_parent ) {
    
          array_push( $exclude, $child->post_parent );
    
        }
    
      } // foreach $child
    
      return $exclude;
    
    } // get_excluded_parents()

    Thanks for talking to you.

    • This reply was modified 5 years, 11 months ago by bcworkz. Reason: code fixed
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Invoer beperken van wp_dropdown_pages’ is closed to new replies.