0

I have a drop down menu that displays a link (the permalink) to all of the posts of a custom post type (county) and would simply like to display their children too this is the code I have so far but get_children is not working...

<ul>
    <?php $menu = new WP_Query( array(
        'post_type' => 'county',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'desc'
    ) );
    while ( $menu->have_posts() ) : $menu->the_post(); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <ul>
            <?php get_children(); ?>
        </ul>
    </li>
    <?php endwhile; ?>
</ul>

this is the code that allows me to assign on custom post type as the parent of another

function show_parent_metabox() {
    parent_select('county');
}

function parent_select ($parent_type) {
    global $post;
    global $wpdb;
    $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = '{$parent_type}'         AND post_status = 'publish' ORDER BY post_title";
    $results = $wpdb->get_results($query, OBJECT);
    echo '<select name="parent_id" id="parent_id">';
    echo '<option value = "">None</option>';
    foreach ($results as $r) {
        echo '<option value="', $r->ID, '"', $r->ID == $post->post_parent ? '    selected="selected"' : '', '>', $r->post_title, '</option>';
    }
    echo '</select>';
}
  • by children, you mean children of posts or what? also can you paste the code for other menu here – Kumar Jan 5 '14 at 13:15
  • Yes @sven i have a bit of code that allows me to assign one custom post type as the parent of another (county as a parent of an area) – jonhodson1984 Jan 5 '14 at 13:17
  • @sven also this is all the code i have i am using standard wp_list_pages() to create my top level navigation and jquery to show the drop down when hovering over a particular nav item – jonhodson1984 Jan 5 '14 at 13:19
  • bit of code that allows me to assign one custom post type as the parent of another, what you actually do in that code? – Kumar Jan 5 '14 at 13:25
  • @sven I have added the parent selector code to the above code. – jonhodson1984 Jan 5 '14 at 13:29
0

Here is the general logic that can be used to fetch the child custom posts

What I've assumed here is, You create a county post, then you create a area post and from parent metabox you select the desired county post and save the post id in post_parent

function wpse_128630_cp( $post_type = 'county', $post_parent = '', $posts_per_page = -1 ){
  $args = array(
    'post_type' => $post_type,
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page,
            'post_parent' => $post_parent ,
    'order' => 'desc'
 );
 $menu = new WP_Query( $args );
 if(!empty($menu)) { return $menu;}
 else{
    //To debug, uncomment below code
    //echo "<pre>";
    //print_r($menu);
    //echo "</pre>";
 }
}
<ul class="wpse-parent menu">
<?php $menu = wpse_128630_cp();
while ( $menu->have_posts() ) : $menu->the_post(); ?>
<li class="menu-item">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php
        $submenu = wpse_128630_cp( 'area', get_the_ID() ); 
        if(!empty($submenu)){ ?>
            <ul class="wpse-sub-menu menu"> <?php
                while ( $submenu->have_posts() ) : $submenu->the_post(); ?>
                    <li class="menu-item">
                        <a href="<?php $submenu->the_permalink(); ?>"><?php $submenu->the_title(); ?></a>
                    </li>
                <?php endwhile; ?>
            </ul><?php
        }?>
  </li>
  <?php endwhile; ?>
  </ul>
  • Do i add the function part of your answer into my functions file or does this all go in my header.php file where my menu code is? – jonhodson1984 Jan 5 '14 at 13:39
  • functions file will do, I'm adding a bit explanation of what I understood from your situation and answering as per that – Kumar Jan 5 '14 at 13:40
  • I may be doing it wrong but this did not work it just returns a completely blank page everywhere. I have separated the function into my functions.php file and everything else in my header.php file in the relevant place. – jonhodson1984 Jan 5 '14 at 13:52
  • I may have been unclear in my question but i would like to return a list of all posts in a custom post type and all of their children. So they display something like this... County > area area area County > area area area – jonhodson1984 Jan 5 '14 at 13:56
  • Childerns post type is same as parent or different ? because you said parent is of cpt county, while children is of cpt area, also you said you get a blank page, does it shows any error, can you check your error logs – Kumar Jan 5 '14 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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