WordPress.org

Forums

[resolved] Get child post titles in list of parent post admin page (4 posts)

  1. bekar09
    Member
    Posted 2 years ago #

    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.

  2. phillbooth
    Member
    Posted 2 years ago #

  3. bekar09
    Member
    Posted 2 years ago #

    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.

  4. bekar09
    Member
    Posted 2 years ago #

    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;
      }

Topic Closed

This topic has been closed to new replies.

About this Topic