Skip Navigation

[Resolved] Slug for post reference field?

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2530743

I am using the following meta query field to filter child posts by post reference.

The field is - hidden link

  $args = array( 
'post_type' => 'child-post-slug',
    'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-project-prompt',
                'value' => $parent_post_ids_array,
                'compare' => 'IN'
            ),
)
     );

$output = get_posts($args);

The child posts exists but the key does not seem to work. Is this the right key for post reference?

#2531141

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

The post relationship field is internally one-to-many post relationship.

To query child posts based on the parent, you will have to use the post relationship API function: toolset_get_related_posts

Here is the reference ticket - can you please try to follow the solution shared with the following related ticket:
- https://toolset.com/forums/topic/query-post-reference-fields/#post-1309527

#2531151

Minesh,

I have tried using the API function but I get an error.

It happens because I am using this inside another query in elementor to query specific posts

This works fine on it's own

    $project_submission_posts_array = toolset_get_related_posts( 
    // get posts related to this one
    array('parent' => $assigned_project_prompt_list_array), 
    // Relationship between the posts
    'project-prompt-to-project-submission', 
    // Additional arguments
    [
        // Get posts where company_id is the parent in given relationship.
        // This is mandatory because we're passing just a single post as the first parameter.
     //   'query_by_role' => 'parent',  
        'role_to_return' => 'child',
        'return' => 'post_id',
      'post_status' => 'publish',
    ]
  );   

This does not work with elementor query :

  add_action( 'elementor/query/project-submission-submitted-for-current-user', function( $query ) {  
   
   $user_id = get_current_user_id();
   $assigned_project_prompt_list_object = get_user_meta($user_id,''field-slug', true);
   if($assigned_project_prompt_list_object[0] != null) {
   $assigned_project_prompt_list_json = $assigned_project_prompt_list_object[0];
     $assigned_project_prompt_list_array = json_decode($assigned_project_prompt_list_json, true);
     
         $project_submission_posts_array = toolset_get_related_posts( 
    // get posts related to this one
    array('parent' => $assigned_project_prompt_list_array), 
    // Relationship between the posts
    'project-prompt-to-project-submission', 
    // Additional arguments
    [
        // Get posts where company_id is the parent in given relationship.
        // This is mandatory because we're passing just a single post as the first parameter.
     //   'query_by_role' => 'parent',  
        'role_to_return' => 'child',
        'return' => 'post_id',
      'post_status' => 'publish',
    ]
  ); 
     
   $query->set( 'post__in', $project_submission_posts_array);
     
     /*
     $args = array( 
    'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-project-prompt',
                'value' => $assigned_project_prompt_list_array,
          //      'type' => 'INT',
                'compare' => 'IN'
            ),
)
     );
     */
  //   $query->set( 'post_status', [ 'publish' ]);
   } // if project_prompt list is not null
   else{
   $query->set( 'post__in', [ '999999' ]);
   }
      
} );
?>

Error thrown:
Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246 Stack trace: #0 /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, NULL) #1 /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(Array, 'project-prompt-...', Array) #2 /home/customer/www/domain/public_html/wp-content/toolset-customizations/filter-project-submission-for-employer-access.php(25): toolset_get_related_posts(Array, 'project-prompt-...', Array) #3 /home/customer/www/domain/public_html/w in /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246

What am I missing?

What is the slug for parent post reference field? is it stored in the child post as a field? something like wpcf--?

I just need that parent post ID but it seems like wpcf-project-prompt is not the right id.

#2531175

Minesh
Supporter

Languages: English (English )

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

What is the slug for parent post reference field? is it stored in the child post as a field? something like wpcf--?
==>
The slug of the relationship will be the exactly the slug of your post-reference field. So, if you have post relationship field created with slug "abc" then the relationship will be also "abc".

What if you try WP_Query way:

For example:

add_action( 'elementor/query/project-submission-submitted-for-current-user', function( $query ) {  
    
   $user_id = get_current_user_id();
   $assigned_project_prompt_list_object = get_user_meta($user_id,''field-slug', true);
   if($assigned_project_prompt_list_object[0] != null) {
   $assigned_project_prompt_list_json = $assigned_project_prompt_list_object[0];
   
$assigned_project_prompt_list_array = json_decode($assigned_project_prompt_list_json, true);

$found = array();
foreach($assigned_project_prompt_list_array  as $k=>$v):
$query = new WP_Query( 
    array(
        'post_type' => 'project-prompt',
        'posts_per_page' => -1,
        'fields'=> 'ids',
        'toolset_relationships' => array(
            array(
                'role' => 'child',
                'related_to' => get_the_ID(),
                'relationship' => 'project-prompt'
            )
        )
       )
);
$found[] = $query->posts;
endforeach;

if(empty($found)){
$query->set( 'post__in', 0);
}else{
$query->set( 'post__in', $found);
}
      
     /*
     $args = array( 
    'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-project-prompt',
                'value' => $assigned_project_prompt_list_array,
          //      'type' => 'INT',
                'compare' => 'IN'
            ),
)
     );
     */
  //   $query->set( 'post_status', [ 'publish' ]);
   } // if project_prompt list is not null
   else{
   $query->set( 'post__in', [ '999999' ]);
   }
       
} );

More info:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

You can adjust the code as required for post-type slugs or relationship slug.

#2534491

My issue is resolved now. Thank you!