Skip Navigation

[Resolved] How to show related posts column in the custom posts admin page

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to add a column to the admin dashboard list for my custom post type, and show related posts in that column.

Solution: There's no built-in way to do it, but you could use the WP API and the Toolset Post Relationships API to set it up with custom code.

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

100% of people find this useful.

This support ticket is created 5 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 12 replies, has 2 voices.

Last updated by justinG-2 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1160787

Hi guys is there any way via a custom to show the related posts created by toolset for a custom post type in the Admin screen of the custom post type?

This makes it easier to manage relations instead of having to go in and check them manually.

#1160903

You mean like adding a column to the list, where you can display another list of related posts? No, there's not a built-in way to do it. You would have to use custom code to create a new column and populate it with your own content using the post relationships API. Documentation for those can be found at the following links:
https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Here's a sample. I can help you with any code that touches the Toolset APIs, if you need assistance.

#1165439

Hi Christian sorry for the late reply. I've been working on a solution and it works partially using the code below. The 'Related Posts'' option shows up in the filter and the column is created with the column label but no related posts show.

I have two post types: Solutions and Usages, connected in a many to many relationship with solution as the parent. The singular name and slug for them is ,strong>solution and strong>usage, respectively. The intermediary post type slug is solution-usage.

I'm guess the issue is in the last function's switch statement or how the array is written. Could you clarify/adapt the code in regards to the post relationship API.

//First add the filter - this appear in the drop-down help section of the Custom post type's admin page 
add_filter('manage_solution_posts_columns', 'my_related_posts_column', 6); 


//Here we create a function to add the column referred to in the filter above

function my_related_posts_column($my_related_posts){
	
	$my_related_posts['toolset_get_related_posts'] = __( 'Related Posts' );
	
	return $my_related_posts;
	
}


//Here we manage the Solutions Post admin page column
add_action('manage_solution_post_custom_column', 'show_my_related_posts_column',6);


//Now we create a function that refers to the action above and displays the related posts in the column
function show_my_related_posts_column($my_related_posts){
	
	switch ($my_related_posts) {
		
		case 'toolset_get_related_posts' :
		
			$usages = toolset_get_related_posts( 
				$solution,
              		array( 
                                'parent' => 'solution', 
                                 'intermediary' => 'solution-usage'
				)
              );
					
					echo $usages;
					break;
								
	}
	
}

#1165647

Okay let's look at the code here first:

add_action('manage_solution_post_custom_column', 'show_my_related_posts_column',6);
function show_my_related_posts_column($my_related_posts){
     
    switch ($my_related_posts) {
         
        case 'toolset_get_related_posts' :
         
            $usages = toolset_get_related_posts( 
                $solution,
                    array( 
                                'parent' => 'solution', 
                                 'intermediary' => 'solution-usage'
                )
              );
                     
                    echo $usages;
                    break;
                                 
    }
     
}

1. The $solution variable is undefined. It should be added as a parameter in the function call so you have access to the solution post ID in the callback, and you should add a 2 in the add_action call params to indicate you will pass 2 params to the callback:

add_action('manage_solution_post_custom_column', 'show_my_related_posts_column', 6, 2 );
function show_my_related_posts_column($my_related_posts, $solution){

2. The toolset_get_related_posts function requires more than two parameters. Here's a guide to the minimum requirements, in order:
- origin post id ( $solution )
- post relationship slug ( your-relationship-slug )
- role of the origin post ( 'parent', 'intermediary', or 'child', depending on the post relationship )
- limit (a positive integer number is required here, I usually use 1000000)
- page # ( usually 0 )
- array of arguments ( usually an empty array() )
- return type ( 'post_id' or 'post_object' )
- role to return ( usually 'child' or 'parent', infrequently 'intermediary')

Here's a code template you can use:

$usages = toolset_get_related_posts(
    $solution,
    'solution-usages-relationship-slug',
    'parent',
    1000000,
    0,
    array(),
    'post_id',
    'child'
);

You should replace solution-usages-relationship-slug with the actual slug of this post relationship. You can find that in Toolset > Relationships if you edit this post relationship. Then the next line down depends on the aliases in this post relationship. If "Solution" is the parent, use 'parent'. If "Solution" is the child, use 'child'. You can determine the aliases in the same edit post relationship screen. Check the checkbox to confirm you want to edit the relationship settings, and you will see the aliases listed. Don't make modifications, just note which is parent and which is child. The next three parameters you probably don't need to modify. Choose 'post_id' or 'post_object' for the next-to-last parameter, depending on what you want to retrieve. The final parameter is the opposite of what you chose for the 3rd parameter.
Now if you chose post_id, when you echo $usages you will probably see something like "Array" because the variable holds an array of post IDs. You should iterate over those using foreach to display each post title or link or whatever you want to show here.

#1165670

Ok, Thanks a lot! It 's much clearer to me now. I will modify my code and test it and then update you with the results.

#1165721

Sounds good, I'll stand by for your update.

#1166427

Hi Christian, I updated my code as shown below and it works showing "array".

Do you have a sample of how the foreach bit would be written?

//First add the filter that we hook into
add_filter('manage_solution_posts_columns', 'my_related_posts_column'); 
  
//Here we create a function to add the column referred to in the filter above. This also appears as a checkbox option in the drop-down screen options section of the Custom post type's admin page 
 
function my_related_posts_column($column){
     
    $column['usages'] = __( 'Related Usages' );
     
    return $column;
     
}
 
 
//Here we manage the Solutions Post admin page column
add_action('manage_solution_post_custom_column', 'show_my_related_posts_column',6,2);
 
 
//Now we create a function that refers to the action above and displays the related posts in the column
function show_my_related_posts_column($column, $solution){
     
    if ($column === 'usages') {
         
            $usages = toolset_get_related_posts( 
                $solution,
                'solution-usage',  
				'parent',
				1000000,
				0,
				array(),
				'post_id',
				'child'
				);
                     
            echo $usages;
                                 
    }
     
}



#1167167

Sure, here is one:

foreach( $usages as $usage ) {
  echo get_the_title( $usage ) . "<br />";
  echo get_post_meta( $usage, 'wpcf-field-slug', true) . "<br />";
}
#1167848

Hi Christian thanks for that, it worked. However I wanted comma separated values, on the same line as opposed to each one on a new line, so I adapted the code a bit to get the result I wanted. At first I tried this:

  foreach( $usages as $usage ) {        
            echo  get_the_title( $usage ) . ", ";      
                                 
    }

But that of course gave me a comma after the last value in the array as well which I didn't want. So then I tried to use php's implode and rtrim functions but those didn't work out in this case either.

I then found this solution on the ACF forums ? hidden link which gave me what I wanted.

if ( count ($usages)) {
				
				foreach ( $usages as $k => $usage){ 
					if($k) echo ', ';
					echo get_the_title($usage);
/*In this statement, $k is a counter that lets us know what item number we are currently working with. 
 This option actually places a comma before each item in the list, rather than after.
  However during the first loop, since  $k is an empty variable and the value will be 0, no comma will be printed 
  at the beginning giving us the appearance that the comma appears after.
 */
				}
     
			}

However how does Views implement comma separated values for arrays? I've notice that when I output a list of terms/categories for a post I am given the option to choose which character I want as a separator.

I may open a separate ticket in regards to this because I am also displaying these related usages in a frontend View as comma-separated values, but these are not terms, so the list has a comma after the last item (which I place manually in the Views content template) which I do not want.

#1167970

However how does Views implement comma separated values for arrays?
Sorry I don't understand what you are asking. It sounds like you're trying to figure out how to iterate over an array to create a commas-separated string of values, or maybe the other way around? Here's an example showing both:

$letters = array();
$string = "A,B,C";
$exp = explode(',', $string);
foreach( $exp as $thisex ) {
  array_push($letters, $thisex);
} // now the $letters array holds three values: "A", "B", and "C"
echo implode($letters, ','); // result:  A,B,C

hidden link
hidden link

#1168897

Hi Christian,

Thanks for the examples ?. I now understand conceptually how implode and explode are supposed to work, however I haven't figured out how to get them to produce a comma separated list of values from my toolset_get_related_posts array.

I tried examples like below but not getting the desired result.

//Since this code  already produces an array
$solutions = toolset_get_related_posts( 
	    //function parameters
	);

//I thought using implode like so 
foreach( $solutions as $solution ) {        
 echo  get_the_title( implode (', ', $solution ) );

//Or so...

foreach( $solutions as $solution ) {        
 echo  implode( ', ',  get_the_title ($solution ) );

//...would work, but i guess its not as straightforward as that.

Any suggestions on how I how would have to use implode to produce a string of comma separated values with toolset_get_related_posts?

I will raise the other question as a separate ticket as though its related to this it involves getting comma separated values with view shortcodes.

#1168998

Try creating a separate array that holds just the post titles. Then implode that array with a comma:

$solutions = toolset_get_related_posts( 
        //function parameters
    );
$titles = array();
foreach( $solutions as $solution ) {        
 $titles[] = get_the_title( $solution );
 }

echo implode($titles, ',');
#1169750

That works Christian. 🙂 I've updated my code with this solution. It will come in very handy in future for displaying related posts.