Skip Navigation

[Resolved] Display related custom post type with custom post status in nested view

This support ticket is created 2 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by yannicN 2 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#2480129

Dear Support Team,

in my WordPress I have created several CPT and additionally defined some custom statuses in the wp-functions.php. I can select these in the toolset forms and can use them throughout WordPress.

I have created a view that shows the different created entities of a CPT from one author. In the loop item of this view, I integrated another view that displays the entities of another CPT that uses a parent-child relationship to display the corresponding entities. All entities with the status "Publish" are displayed correctly. However, if an entity has a custom status, that entity is no longer displayed.

Unfortunately, I can't explain myself how this happens. Can you help me?

#2480437

Hello,

It seems to be a custom codes issue, please provide detail steps to reproduce the same problem:
How do you define some custom statuses in the wp-functions.php?

I need to test and debug it in my localhost, thanks

#2480573

Hello,

here is the code for defining the custom statuses:

add_action( 'init', 'add_custom_post_status', 1 );
function add_custom_post_status(){

register_post_status( 'abgelehnt', array(
'label' => _x( 'Abgelehnt', 'post' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Abgelehnt <span class="count">(%s)</span>', 'Abgelehnt <span class="count">(%s)</span>' ),
));

register_post_status( 'angenommen-zahlung', array(
'label' => _x( 'Angenommen / Zahlung ausstehend', 'post' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Angenommen / Zahlung ausstehend <span class="count">(%s)</span>', 'Angenommen / Zahlung ausstehend <span class="count">(%s)</span>' ),
));

register_post_status( 'abgeschlossen', array(
'label' => _x( 'Angenommen / Zahlung erfolgt', 'post' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Angenommen / Zahlung erfolgt <span class="count">(%s)</span>', 'Angenommen / Zahlung erfolgt <span class="count">(%s)</span>' ),
));

}

#2482235

I have tried your custom codes in below test site:
hidden link

Put your custom codes into theme file "functions.php":
hidden link
Lines 632~662

Create a post:
hidden link
I don't see those custom post status, please try to reproduce the same problem in above test site, thanks

#2482345

Dear Luo,
I can't access your test site, because I have no credentials to login. You can reproduce the error like this:

1. Create two custom post types
2. Integrate a one-to-many relationship between these two
3. Create a view for the parent post type
4. Create a second view for the child post type
5. Set the filter to display entrys that are related to the parent custom post type in the loop and set the filter to display all status
6. Integrate the second view with the child post type in the loop of the first view of parent post type
7. Now the parent entrys will displayed and under the parent entrys, the child entrys are visible
8. If you set a child entry to a custom post status, you can see, that the child entry isn't visible

Here are the full code to integrate the custom status an display these also in WordPress backend:

add_action( 'init', 'add_custom_post_status', 1 );
function add_custom_post_status(){
	
	register_post_status( 'vermittelt', array(
		'label'                     => _x( 'Vermittelt', 'post' ),
		'public'                    => true,
		'exclude_from_search'       => false,
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'label_count'               => _n_noop( 'Vermittelt <span class="count">(%s)</span>', 'Vermittelt <span class="count">(%s)</span>' ),
	));
	
	register_post_status( 'abgelaufen', array(
		'label'                     => _x( 'Abgelaufen', 'post' ),
		'public'                    => true,
		'exclude_from_search'       => false,
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'label_count'               => _n_noop( 'Abgelaufen <span class="count">(%s)</span>', 'Abgelaufen <span class="count">(%s)</span>' ),
	));
	

	register_post_status( 'abgelehnt', array(
		'label'                     => _x( 'Abgelehnt', 'post' ),
		'public'                    => true,
		'exclude_from_search'       => false,
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'label_count'               => _n_noop( 'Abgelehnt <span class="count">(%s)</span>', 'Abgelehnt <span class="count">(%s)</span>' ),
	));
	

	register_post_status( 'angenommen-zahlung', array(
		'label'                     => _x( 'Angenommen / Zahlung ausstehend', 'post' ),
		'public'                    => true,
		'exclude_from_search'       => false,
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'label_count'               => _n_noop( 'Angenommen / Zahlung ausstehend <span class="count">(%s)</span>', 'Angenommen / Zahlung ausstehend <span class="count">(%s)</span>' ),
	));
	

	register_post_status( 'abgeschlossen', array(
		'label'                     => _x( 'Angenommen / Zahlung erfolgt', 'post' ),
		'public'                    => true,
		'exclude_from_search'       => false,
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'label_count'               => _n_noop( 'Angenommen / Zahlung erfolgt <span class="count">(%s)</span>', 'Angenommen / Zahlung erfolgt <span class="count">(%s)</span>' ),
	));
	
}

add_action( 'post_submitbox_misc_actions', 'add_status_to_dropdown_vermittelt');
function add_status_to_dropdown_vermittelt()
{
global $post;
if(($post->post_type != 'sendung') && ($post->post_type != 'fahrt'))
return false;
$status = ($post->post_status == 'vermittelt') ? "jQuery( '#post-status-display' ).text( 'Vermittelt' ); jQuery( 
'select[name=\"post_status\"]' ).val('vermittelt');" : '';
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"post_status\"]' ).append( '<option value=\"vermittelt\">Vermittelt</option>' );
".$status."
});
</script>";
}

add_action( 'post_submitbox_misc_actions', 'add_status_to_dropdown_abgelaufen');
function add_status_to_dropdown_abgelaufen()
{
global $post;
if(($post->post_type != 'sendung') && ($post->post_type != 'fahrt'))
return false;
$status = ($post->post_status == 'abgelaufen') ? "jQuery( '#post-status-display' ).text( 'Abgelaufen' ); jQuery( 
'select[name=\"post_status\"]' ).val('abgelaufen');" : '';
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"post_status\"]' ).append( '<option value=\"abgelaufen\">Abgelaufen</option>' );
".$status."
});
</script>";
}

add_action( 'post_submitbox_misc_actions', 'add_status_to_dropdown_zahlung_ausstehend');
function add_status_to_dropdown_zahlung_ausstehend()
{
global $post;
if(($post->post_type != 'fahrtangebot') && ($post->post_type != 'sendungsangebot'))
return false;
$status = ($post->post_status == 'angenommen-zahlung') ? "jQuery( '#post-status-display' ).text( 'Angenommen / Zahlung ausstehend' ); jQuery( 
'select[name=\"post_status\"]' ).val('angenommen-zahlung');" : '';
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"post_status\"]' ).append( '<option value=\"angenommen-zahlung\">Angenommen / Zahlung ausstehend</option>' );
".$status."
});
</script>";
}

add_action( 'post_submitbox_misc_actions', 'add_status_to_dropdown_zahlung_erfolgt');
function add_status_to_dropdown_zahlung_erfolgt()
{
global $post;
if(($post->post_type != 'fahrtangebot') && ($post->post_type != 'sendungsangebot'))
return false;
$status = ($post->post_status == 'abgeschlossen') ? "jQuery( '#post-status-display' ).text( 'Angenommen / Zahlung erfolgt' ); jQuery( 
'select[name=\"post_status\"]' ).val('abgeschlossen');" : '';
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"post_status\"]' ).append( '<option value=\"abgeschlossen\">Angenommen / Zahlung erfolgt</option>' );
".$status."
});
</script>";
}

add_action( 'post_submitbox_misc_actions', 'add_status_to_dropdown_abgelehnt');
function add_status_to_dropdown_abgelehnt()
{
global $post;
if(($post->post_type != 'fahrtangebot') && ($post->post_type != 'sendungsangebot'))
return false;
$status = ($post->post_status == 'abgelehnt') ? "jQuery( '#post-status-display' ).text( 'Abgelehnt' ); jQuery( 
'select[name=\"post_status\"]' ).val('abgelehnt');" : '';
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"post_status\"]' ).append( '<option value=\"abgelehnt\">Abgelehnt</option>' );
".$status."
});
</script>";
}

add_action('admin_footer-edit.php','status_add_in_quick_edit_vermittelt_abgelaufen');
function status_add_in_quick_edit_vermittelt_abgelaufen() {
global $post;
if(($post->post_type != 'sendung') && ($post->post_type != 'fahrt'))
return false;
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"vermittelt\">Vermittelt</option>' );
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"abgelaufen\">Abgelaufen</option>' );
});
</script>";
}

add_action('admin_footer-edit.php','status_add_in_quick_edit_angenommen_abgelehnt');
function status_add_in_quick_edit_angenommen_abgelehnt() {
global $post;
if(($post->post_type != 'fahrtangebot') && ($post->post_type != 'sendungsangebot'))
return false;
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"angenommen-zahlung\">Angenommen / Zahlung ausstehend</option>' );
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"abgeschlossen\">Angenommen / Zahlung erfolgt</option>' );
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"abgelehnt\">Abgelehnt</option>' );
});
</script>";
}

function display_status_in_list( $states ) {
	global $post;
	$arg = get_query_var( 'post_status' );
		if($post->post_status == 'vermittelt'){
			echo "<script>
			jQuery(document).ready( function() {
				jQuery( '#post-status-display' ).text( 'Vermittelt' );
			});
		</script>";
		return array('Vermittelt');
		}
		if($post->post_status == 'abgelaufen'){
			echo "<script>
			jQuery(document).ready( function() {
				jQuery( '#post-status-display' ).text( 'Abgelaufen' );
			});
		</script>";
		return array('Vermittelt');
		}
		if($post->post_status == 'angenommen-zahlung'){
			echo "<script>
			jQuery(document).ready( function() {
				jQuery( '#post-status-display' ).text( 'Angenommen / Zahlung ausstehend' );
			});
		</script>";
		return array('Angenommen / Zahlung ausstehend');
		}
		if($post->post_status == 'abgeschlossen'){
			echo "<script>
			jQuery(document).ready( function() {
				jQuery( '#post-status-display' ).text( 'Angenommen / Zahlung abgeschlossen' );
			});
		</script>";
		return array('Angenommen / Zahlung abgeschlossen');
		}
		if($post->post_status == 'abgelehnt'){
			echo "<script>
			jQuery(document).ready( function() {
				jQuery( '#post-status-display' ).text( 'Abgelehnt' );
			});
		</script>";
		return array('Abgelehnt');
		}
		if($post->post_status == 'publish'){
			echo "<script>
			jQuery(document).ready( function() {
				jQuery( '#post-status-display' ).text( 'Veröffentlicht' );
			});
		</script>";
		return array('Veröffentlicht');
		}
	return $states;
}
add_filter( 'display_post_states', 'display_status_in_list' );
#2482805

Thanks for the details, I am trying to reproduce the same problem, will update here if find anything

#2482865

It is possible with custom codes, see below test site:
hidden link

Result page:
hidden link

Detail steps:
1) Parent post view:
hidden link

2) Child post view:
hidden link
Don't setup the relationship filter in it

3) Add below custom codes into your theme file "functions.php":

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 101, 3 );
 
function prefix_show_only_current_author( $query_args, $settings, $view_id ) {
    if ( in_array($view_id, array(21) ) ) { //Replace 21 with your child post view ID
        $query = new WP_Query( 
			 array(
				 'post_type' => 'fahrtangebot',
				 'posts_per_page' => -1,
				 //new toolset_relationships query argument
				 'toolset_relationships' => array(
					 'role' => 'child',
					 'related_to' => get_the_ID(),
					 // this will work only with relationships that have existed before the migration
					 // if possible, use the relationship slug instead of an array
					 'relationship' => 'sendung-fahrtangebot'
				 ),
				 'fields' => 'ids',
			 )
		);
		$query_args['post__in'] = $query->posts;
    }
    return $query_args;
}
#2484597

Thanks for your solution. It works well, but there is a new problem. The view shows all fahrtangebots, if a parent sendung has no connected fahrtangebot. I customized it in your develop-area, so you can see the situation.

#2485031

I have modified the PHP codes as below:


add_filter( 'wpv_filter_query', 'custom_status_func', 101, 3 );
 
function custom_status_func( $query_args, $settings, $view_id ) {
    if ( in_array($view_id, array(21) ) ) {
        $query = new WP_Query( 
			 array(
				 'post_type' => 'fahrtangebot',
				 'posts_per_page' => -1,
				 //new toolset_relationships query argument
				 'toolset_relationships' => array(
					 'role' => 'child',
					 'related_to' => get_the_ID(),
					 // this will work only with relationships that have existed before the migration
					 // if possible, use the relationship slug instead of an array
					 'relationship' => 'sendung-fahrtangebot'
				 ),
				 'fields' => 'ids',
			 )
		);
		if(!$query->posts){
			$query->posts = array(-1);
		}
		$query_args['post__in'] = $query->posts;
    }
    return $query_args;
}

Please test again, check if it is fixed, thanks

#2485147

My issue is resolved now. Thank you!