Skip to content

Deleting a block or template does not delete postmeta #45

Description

@tharsheblows

When a block or template is deleted, the postmeta is not deleted. This is an issue because in templates, the data will be retrieved using get_post_meta.

Reproduce:

  • make a template
  • make a post, fill out the template and save
  • go back into the post, delete the block then save

You can keep an eye on the postmeta table to see that it's still there or put a filter on the content to view it (at the bottom)

There are a couple of ways of fixing:

  • delete postmeta on block deletion and delete all instances of the meta_keys used in a template on template deletion (so as not to leave orphaned uneditable/undeletable data)
  • use a dynamic render which won't show the postmeta data unless the block is there

I think the first is the way to go as it avoids the orphaned data issue.

Cheap and cheerful content filter:

function mjj_filter_content( $content ){
	$post_id = get_the_ID();
	if( empty( $post_id ) ){
		return $content;
	}
	$gcf_postmeta = get_post_meta( $post_id, 'main field', true );
	if( empty( $gcf_postmeta ) ){
		return $content;
	}
	return '<h2>' . print_r( $gcf_postmeta, true ) . '</h2>' . $content;
}
add_filter( 'the_content', 'mjj_filter_content' );

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions