WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Get child post titles in list of parent post admin page

[Resolved] Get child post titles in list of parent post admin page

  • I have a custom post type Book and a child custom post type (to book) called Person. A book has for example 2 persons associated viz. Beck and Mike. Beck is the Author and Mike is the Editor. Author and Editor are custom taxonomies (called Profession) associated with Person custom post type.

    Now in the list of Books (admin screen) I want to have 2 sortable columns Author and Editor which should list all the author and editors associated with a Book. How can I achieve this? I am using Types plugin and Codepress Admin Columns plugin.

    If this is not possible can I display all Persons associated with a book with the below code? Well even this snippet isn’t working. Can somebody point me what I am doing wrong?

    case "persons":
            query_posts( array(
                'post_type' => 'books',
                'meta_query' => array(
                    array(
                        'key' => '_wpcf_belongs_books_id',
                        'value' => $post_id,
                        'compare' => '==',
                        'type' => 'numeric',
                    ),
                )
            ) );
     
            if (have_posts()) :
                while (have_posts()) : the_post();
                    $post_id = get_the_ID() ;
                    echo the_title();
                endwhile;
            else :
                echo 'Nothing';
            endif;
     
          break;
      }

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • can you give a small example of wp_list_pages being used to populate admin column?

    I cannot directly use “child_of” directly because the posts are not direct children. There are two different post types; one being associated to the other based on a custom field.

    Please read my requirement once again and let me know if its not clear.

    Forget it. I figured out the problem. Pasting the correct snippet for people who want similar functionality.

    case "persons":
            $persons = query_posts( array(
                'post_type' => 'persons',
                'meta_query' => array(
                    array(
                        'key' => '_wpcf_belongs_books_id',
                        'value' => $post_id,
                        'compare' => '==',
                        'type' => 'numeric',
                    ),
                )
            ) );
    
            if ( !empty( $persons ) ) {
    			$out = array();
    			foreach ( $persons as $person ) {
    					$out[] = sprintf( '<a href="%s">%s</a>',
    						esc_url( add_query_arg( array( 'post' => $person->ID, 'action' => 'edit' ), 'post.php' ) ),
    						esc_html( $person->post_title )
    					);
    				}
    
    				/* Join the terms, separating them with a comma. */
    				echo join( __( ', ' ), $out );
    		}
    
          break;
      }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Get child post titles in list of parent post admin page’ is closed to new replies.
Skip to toolbar