Skip Navigation

[Resolved] Delete all posts that have the same Field Value when deleting one of them

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

Problem:

When i am at Post 1 with slug 1234, i can create Post 2 where the slug of the Post 1 which is 1234 will be saved in the custom field "custum-id-1" of Post 2.

Solution:

There isn't such a built-in feature within Toolset, you might need to consider custom codes, for example:
1) When user delete a post, use WordPress action hook "before_delete_post" to trigger a PHP function

2) In this function, find those posts are related, and delete the related posts:

Relevant Documentation:

https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post

https://codex.wordpress.org/Function_Reference/get_posts

https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

https://codex.wordpress.org/Function_Reference/wp_delete_post

This support ticket is created 5 years, 10 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 4 replies, has 2 voices.

Last updated by Nashaat 5 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1194108

I have a Cred form with a field that saves the slug each time a post is created in another post.

for example:

Post 1 has slug="1234"

when i am at Post 1 with slug 1234, i can create Post 2 where the slug of the Post 1 which is 1234 will be saved in the custom field "custum-id-1" of Post 2.

Kind of relationship but not Toolset relationship

The field is saving the slug like following: [cred_field field="secret-id-1" force_type="field" class="form-control" output="bootstrap" value="[wpv-post-slug item='$current_page']"]

now when deleting one of the Posts i want to get the slug of the deleted post and search if any another post has the name of that slug as value in the custom-id-1 field and delete it ad well. This means if Post 1234 was deleted, the Post 2 should get deleted.

How can i achieve this?

#1194127

Hello,

There isn't such a built-in feature within Toolset, you might need to consider custom codes, for example:
1) When user delete a post, use WordPress action hook "before_delete_post" to trigger a PHP function:
https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post

2) In this function, find those posts are related,
https://codex.wordpress.org/Function_Reference/get_posts
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

And delete the related posts:
https://codex.wordpress.org/Function_Reference/wp_delete_post

For your reference.

#1194505

Thanks for your help. I have tried to add following code:

<?php
/**
 * New custom code snippet.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_action('before_delete_post', 'delete_all_post_with_same_slug_field_value');
function delete_all_post_with_same_slug_field_value ($postId) {
	$post = get_post($postId);
  	$slug = explode('__trashed', $post->post_name)[0];
	$query = new WP_Query([
		'post_type' => 'folder',
		'meta_query' =>	[[
          	'key' => 'wpcf-secret-id-1',
			'value'	=>	$slug
	    ]]
    ]);
  	if($query->have_posts()) {
      while($query->have_posts()) {
        wp_delete_post(get_the_ID(), true);
      }
    } 
}

i am trying to to do following:

1- get post id before deleting
2- get slug of the post that getting deleted
3- run a query to look for another posts in the same post type, that has in in the custom field "custom-id-1" the same value as $slug of the post that getting deleted. if its true it should delete them as well.

unfortunately i am not getting this to work. is it actually the right way?

#1194517

I tried also this but still not working any suggestions?

<?php
/**
 * New custom code snippet.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );


add_action('before_delete_post', 'my_before_delete_post');
function my_before_delete_post($postid)
{
    $args  = array(
        'post_type' => array(
            'folder'
        ),
        'meta_query' => array(
            array(
                'key' => 'wpcf-secret-id-1',
                'value' => $postid
            )
        )
    );
    // The Query
    $query = new WP_Query($args);
    // The Loop
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            wp_delete_post(get_the_ID());
        }
    }
    wp_reset_postdata();
}
#1194718

My issue is resolved now. Thank you!

I have asked for solution on stackoverflow. https://stackoverflow.com/questions/54481931/when-deleting-one-post-delete-all-relevant-posts/

here is my final code for this.

add_action('before_delete_post', 'my_before_delete_post');
function my_before_delete_post($postid)
{
    $args  = array(
        'post_type' => array(
            'add-your-post-type-here'
        ),
        'meta_query' => array(
            array(
                'key' => 'wpcf-custum-id-1',
                'value' => get_post_field( 'post_name', $postid )
            )
        )
    );
    // The Query
    $query = new WP_Query($args);
    // The Loop
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            wp_delete_post(get_the_ID());
        }
    }
    wp_reset_postdata();
}

OR you can use following code which do the same

add_action('before_delete_post', 'delete_all_post_with_same_slug_field_value');
function delete_all_post_with_same_slug_field_value ($postId) {
	$post = get_post($postId);
  	$slug = explode('__trashed', $post->post_name)[0];
	$query = new WP_Query([
		'post_type' => array('add-your-post-type-here', 'add-your-post-type-here'),
		'meta_query' =>	[[
          	'key' => 'wpcf-custum-id-1',
			'value'	=>	$slug
	    ]]
    ]);
  	foreach ($query->posts as $post) {
      wp_delete_post($post->ID, true);
    }
}

Change add-your-post-type-here to the name of your custom post type.
change wpcf-custum-id-1 to be the name of your custom field. if you are using toolset custom fields, you will need to keep wpcf- and just replace custum-id-1 with the name of your field.