Skip Navigation

[Resolved] Filter Based on Shortcode Attribute in Queried Post Equalling Current Post ID

This support ticket is created 7 years, 1 month 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.

Our next available supporter will start replying to tickets in about 1.94 hours from now. Thank you for your understanding.

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)

Tagged: 

This topic contains 11 replies, has 3 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#581967

My site has 6 CPTs as follows...

Vessel
is a parent of
Voyage
is a parent of
Ticket
is a parent of
Variation
is a parent of
Booking
is a parent of
Passenger

What I'd like to do is create a View in the Voyage CPT that shows Passengers for a specific Voyage.

I have been able to do this with a number of nested views but that creates the problem of not being able to do anything beyond display a list, such as counting results and titling the view only if results are found (Tickets, Variations and Bookings will likely always be found but Passengers may not).

Currently in the Passenger CPT I use a custom shortcode of [passenger_view-voyage /] to output the voyage name (code below). Now, I'm wondering if the Passenger View in the Voyage CPT could query the result of a shortcode like this for the Passenger Post and show the post only if the query result equals the current Voyage Post ID?

// Create shortcode to output the Voyage name for a Passenger with option of including url link to Voyage
function passenger_great_great_grandparent_shortcode($atts) {
   $passenger_id = get_the_ID();
   $booking_id = get_post_meta($passenger_id, '_wpcf_belongs_booking_id', true);
   $variation_id = get_post_meta($booking_id, '_wpcf_belongs_ticket-variation_id', true);
   $ticket_id = get_post_meta($variation_id, '_wpcf_belongs_ticket_id', true);
   $voyage_id = get_post_meta($ticket_id, '_wpcf_belongs_voyage_id', true);
   $voyage_name = get_post_meta($voyage_id, 'wpcf-voyage-name', true);
   $voyage_url = get_permalink($voyage_id);
   shortcode_atts( array(
    'url' => 'true',
), $atts );
   $prefix = "";
   $suffix = "";
   if ('true' == $atts['url']) {
     $prefix = "<a href=".$voyage_url.">";
     $suffix = "</a>";
   } 
   return $prefix . $voyage_name . $suffix;

//   return '<a href=".$voyage_url.">' .$voyage_name. '</a>';
}
add_shortcode( 'passenger_view-voyage', 'passenger_great_great_grandparent_shortcode' );
#582289

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes - I think its possible but I would like to know where you want to display passenger information - on single Voyage post page?

#582391

Yes, I would like to display it on the Single Voyage post page

#582401

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - I think you can use the same function but you may need to change few things.

Could you please setup test example of Single Voyage post page and related passenger information and send me all details that which passengers should show up on the that single voyage post and create a view to display passengers and send me all these details and then I will take a look at it.

#582409

If you go to hidden link you will see a section titled Passenger Info View.

This is using the nested views method mentioned above. It currently displays the correct passenger info for that voyage.

Do you need to send a request form for system access?

#582731

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - with the link you shared it ask me for login. Could you please share access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#582733

Please send private form request again, I mistakenly deleted after sending details!

#582734

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry - I didnt created any user.

#582735

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#585164

It needs custom codes, here are detail steps:
1) Create a shortcode to get all related passenger posts, add below codes into your theme/functions.php

function passenger_ids_from_voyage_func($atts) {
	$atts = shortcode_atts( array(
		'voyage_id' => 0,
	), $atts );
	
	if(!$atts['voyage_id']){
		$voyage_id = get_the_ID();
	}
	$voyages = array($voyage_id);
	
	$tickets = get_child_ids($voyages, 'ticket', 'voyage');
	$variations = get_child_ids($tickets, 'variation', 'ticket');
	$bookings = get_child_ids($variations, 'booking', 'variation');
	$passengers = get_child_ids($bookings, 'passenger', 'booking');
	
	$res = '';
	if(is_array($passengers)){
		$res = implode(',', $passengers);
	}
	return $res;
}

add_shortcode( 'passenger_ids_from_voyage', 'passenger_ids_from_voyage_func' );

function get_child_ids($parent_id = 0, $child_slug = '', $parent_slug = ''){
	$field_slug = '_wpcf_belongs_' . $parent_slug . '_id';
	$args = array(
		'post_type' => $child_slug,
		'numberposts' => -1,
		'meta_query' => array(
			'key' =>  $field_slug,
			'value' => $parent_id,
			'compare' => 'IN',
			
		),
		'fields' => 'ids',
	);
	$res = get_posts($args);
	return $res;
}

2) Dashboard-> Toolset-> Settings-> Front-end Content
option "Third-party shortcode arguments" add above shortcode name "passenger_ids_from_voyage"

3) create a view list "passenger" posts, filter with:
Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]
hidden link

4) In the content template display above view, like this:
[wpv-view name="passenger-lists" ids='[passenger_ids_from_voyage]']

5) Test the result here:
hidden link

Please check if it is what you want, thanks

#586429

I have taken a look and found that this View outputs the same results on every Voyage page whereas it should just show bookings for the single Voyage being shown.

I suspect I know what may be causing the issue: from what I can tell in the code you have given, you have collected the Voyage IDs as an array which means that the output will be for multiple Voyages.

If I'm right, what we need as the first step in that code (I think) is to collect the Ticket IDs as an array under a SINGLE Voyage (the current one in the loop).

Does that make sense?

#586547

Yes, you are right, it collect the Ticket IDs as an array under a SINGLE Voyage, then variation IDs... passengers IDs,

I just modified the PHP codes to below:

function passenger_ids_from_voyage_func($atts) {
    $atts = shortcode_atts( array(
        'voyage_id' => 0,
    ), $atts );
     
    if(!$atts['voyage_id']){
        $voyage_id = get_the_ID();
    }
    $voyages = array($voyage_id);
     
    $tickets = get_child_ids($voyages, 'ticket', 'voyage');
	
    $variations = get_child_ids($tickets, 'ticket-variation', 'ticket');
	
    $bookings = get_child_ids($variations, 'booking', 'ticket-variation');
	
    $passengers = get_child_ids($bookings, 'passenger', 'booking');
	
    if(!is_array($passengers)){
		$passengers = array(0);
	}
    $res = '';
	$res = implode(',', $passengers);
	
    return $res;
}
 
add_shortcode( 'passenger_ids_from_voyage', 'passenger_ids_from_voyage_func' );
 
function get_child_ids($parent_ids = array(), $child_slug = '', $parent_slug = ''){
	if(!$parent_ids){
		return;
	}
    $field_slug = '_wpcf_belongs_' . $parent_slug . '_id';
    $args = array(
        'post_type' => $child_slug,
        'numberposts' => -1,
        'meta_query' => array(
			array(
				'key' =>  $field_slug,
				'value' => $parent_ids,
				'compare' => 'IN',
             ),
        ),
        'fields' => 'ids',
    );
	
    $res = get_posts($args);
	
    return $res;
}

Then create a new voyage post, test it in front-end:
hidden link

See the result, please check if it is want you want, thanks