Title: PHP Code for Retrieving Posts URLs in Trash
Last modified: June 26, 2022

---

# PHP Code for Retrieving Posts URLs in Trash

 *  Resolved [Bilal Ahmad](https://wordpress.org/support/users/techmaish/)
 * (@techmaish)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-retrieving-posts-urls-in-trash/)
 * I have removed around 500 Posts and they are in Trash. I want to retrieve only
   URLs of these posts so that I can submit to Google for removal.
 * Kindly guide me on what PHP Code I will need to retrieve only these URLs.
 * Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-retrieving-posts-urls-in-trash/#post-15776598)
 * Hi [@techmaish](https://wordpress.org/support/users/techmaish/),
 * It looks like `WP_Query` accepts a `status` argument that would allow you to 
   fetch posts from the trash only: [https://developer.wordpress.org/reference/classes/wp_query/#status-parameters](https://developer.wordpress.org/reference/classes/wp_query/#status-parameters)
 * Unfortunately, trashed posts don’t seem to have the same permalinks as when they
   were published, so there’s some massaging of the permalink required.
 * You should be able to achieve what you’re looking for with the following code:
 *     ```
       add_action( 'admin_notices', function () {
       	$trashed_posts = get_posts( array( 'post_status' => 'trash' ) );
       	echo '<p><pre>';
   
       	foreach ( $trashed_posts as $post ) {
       		// trick get_permalink into thinking this is a visible post, otherwise we won't get the pretty permalink.
       		$post->post_status = 'publish';
       		echo esc_html( str_replace( '__trashed', '', get_permalink( $post ) ) ), '<br>';
       	}
   
       	echo '</pre></p>';
       } );
       ```
   
 * The list of posts will show up in your admin area. Feel free to only add this
   as a run-once snippet.
 *  Thread Starter [Bilal Ahmad](https://wordpress.org/support/users/techmaish/)
 * (@techmaish)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-retrieving-posts-urls-in-trash/#post-15779040)
 * Thank you Shea.
 * I just added the code to my Thesis Theme Custom.php file with the addition of
   the following code.
    if ( ! is_page( ‘PageID’ ) ) return; Added the Page ID to
   retrieve the data on this page but nothing is appearing on the page.
 * I am sorry but I am not good at PHP.
    Can you guide me further please.
 *  Thread Starter [Bilal Ahmad](https://wordpress.org/support/users/techmaish/)
 * (@techmaish)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/php-code-for-retrieving-posts-urls-in-trash/#post-15779057)
 * And if I use the Code in Code Snippest Plugin, where can I see the retrieved 
   results than?
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [3 years, 10 months ago](https://wordpress.org/support/topic/php-code-for-retrieving-posts-urls-in-trash/#post-15855241)
 * They should appear on the page at the top of the screen. No need to amend the
   code, just add it as a Run Once snippet.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘PHP Code for Retrieving Posts URLs in Trash’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [trash](https://wordpress.org/support/topic-tag/trash/)
 * [urls](https://wordpress.org/support/topic-tag/urls/)

 * 4 replies
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [3 years, 10 months ago](https://wordpress.org/support/topic/php-code-for-retrieving-posts-urls-in-trash/#post-15855241)
 * Status: resolved