Skip Navigation

[Resolved] Counting Post Relationships

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.

This topic contains 1 reply, has 2 voices.

Last updated by Caridad 5 years, 6 months ago.

Assigned support staff: Minesh.

Author
Posts
#274991

I am trying to query a custom post type for a date range and count/further query the number of both parent and child post-type relationships.

I have the post type Fulfillments that has a:
Parent post type relationship: transport
Child post type relationship: restaurants-to-order

restaurants-to-order post type has:
Parent post type relationship: fulfillment
Parent post type relationship: restaurant

So I want to look at fulfillments for a date range (January, 2014 for example) and count:
- the name of each transport and how many fulfillments they had
- the name of each restaurant and how many fulfillments they had

What I have so far:

//Set Date Range
$monthnum = '1';
$year = '2014';

//Get List of Fulfillments within range
$args = array(
	'post_type'              => 'fulfillments',
	'monthnum'				=> $monthnum,
	'year' 						=> $year
);
$query1 = new WP_Query( $args );
$fulfillments = $query1->get_posts();

 $fullfillid = array();
//Extract Post ID's
foreach($posts1 as $post){
        $fullfillid[] = $post->ID;
}

$drivers = array();
$restaurants = array();

foreach($fullfillid as $fulfillment){
	//Get Driver ID
	//NEED CODE TO GET PARENT POST TYPE ID
	
	$parentID = '';
	
	//Store driver ID in array
	$drivers[] = $parentID;
	
	
	//Get Restaurant-Order ID
	$childargs = array(
	'post_type' => 'restaurant-to-order',
	'meta_query' => array(array('key' => '_wpcf_belongs_property_id', 'value' => $fulfillment()))
	);
	$child_posts = get_posts($childargs);
	
	foreach($child_posts as $post){
        $restorderid[] = $post->ID;
	}
	
	//Get Restaurant ID from restaurant-to-order
	foreach($restorderid as $restorder){
		//NEED CODE TO GET PARENT POST TYPE ID
		
		$parentID = '';
		
		$restaurants[] = $parentID;
	}
}

As seen in the above code snippet the main thing I'm missing is how to get the ID of the parent post type relationship from the ID of the child. If you can show me how to get that I should be right from there.

#275156

Transport is the parent for fulfillments, I understand the Driver ID is this one:

// Get driver id
$driver_id = get_post_meta( $fulfillment, '_wpcf_belongs_transport_id', true );

The key is to replace the 'transport' part with the slug of the parent post type.

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,
Caridad