finally I find right code with helping by ChatGPT
DELETE p
FROM wp_posts p
INNER JOIN wp_term_relationships tr1 ON (p.ID = tr1.object_id AND tr1.term_taxonomy_id = 2)
LEFT JOIN wp_term_relationships tr2 ON (p.ID = tr2.object_id AND tr2.term_taxonomy_id = 20)
WHERE p.post_type="post"
AND p.post_status="publish"
AND DATEDIFF(NOW(), p.post_date) > 180
AND tr2.term_taxonomy_id IS NULL;
Lets try another way to write new code. consider I want to delete all
posts from category ID 2 in time duration more than 180 days except
those post that are in category ID 20 too . I mean there are for
example 2000 posts in category ID 2 in time duration +180 days and 500
posts of them are in category ID 20 too. so I expect just 1500 posts
will delete and keep those 500 posts.
I try this code and it is working fine
Related Posts:
- Delete WordPress posts from URL list sql query
- How can I get posts from 9/13/11 to today with SQL? [closed]
- Adding wordpress posts using phpmyadmin
- Export WordPress Posts and Meta Information in CSV format
- Manually delete post from database
- how to get permalink using sql
- Use Cron to modify posts via sql
- Sql syntax error
- get posts and postmetas in assoc array
- How do I find which articles are missing a featured image in The WordPress database?
- Bulk update post slugs through database
- Get the most repeated post contents from a search?
- Multiples Empty Posts were created and cannot delete them
- SQL query to retrieve all the records that have not indicated category
- How to verify wp user password by sql query in wp? [closed]
- Get ALL post ID’s export list (Only id’s. Php or sql not important )
- MySql query to get posts with all meta and terms
- Bulk delete WordPress posts with phpMyAdmin
- WordPress website loads more posts than expected
- SQL Command for restoring trashed comments
- How to Assign / Move all Deleted post to a category
- Migrate posts from category and sub-category via SQL
- Need to change all links in page content but not in post content
- SQL Bulk Move old posts by one author to another category
- Export all posts to import as product items
- Change published date and time using SQL query for page in every 5 hour
- SQL DELETE multiple post_meta on single and multi-sites
- Bulk find & replace on WordPress posts/pages (minus image paths)
- SQL query: select duplicate posts – but using duplicate meta [duplicate]
- WordPress MySQL Search and replace with wildcard?
- Using a meta_value or postdate to to query and orderby
- insert post fails after multiple postings
- How to run select query of post with category and tags?
- get backup file from wordpress database in x days
- Export Posts with specific custom fields by sql
- Query to get data of a post, if in category?
- Can’t create posts with WP DB Abstraction Plugin
- SQL query to remove first image in all posts
- Delete junk text from all the post using my sql command
- Special Query: Title, Terms, Content – %LIKE%
- If meta_key exists then don’t select this post?
- The page that displays Posts on homepage
- Change permalinks in posts via SQL
- Batch Scanning and Deleting Empty WordPress Posts
- How to get a list of all posts and their categories?
- How to speed up WP_Posts_List_Table __construct() Query on a wordpress website with over 1 million posts?
- disable password protected page for logged users
- How do I use inline SVG in WordPress
- “Uncaught SyntaxError: Unexpected token
- Pull posts from another wordpress install on same server
- How can I get all posts data from within a paginated search result?
- Syndicated Posts when Updated Lose HTML tags
- How to insert current date/time in the content of a post?
- I want to query posts in monthly basis
- White Blank Page when Updating and Publishing Page/Post
- Full Width Main Post
- Posts vs. Pages
- Plain permalinks not working!
- How can I find out what items a user has purchased? [closed]
- Nothing Found error when creating new posts – how to correct this?
- Display multiple custom taxonomy values on single custom post types page?
- Underscores Theme Unit Testing – Catching Untitled Posts
- How to do set post permalinks using 6 digit random unique function?
- Encountering “Wrong nonce. Action prohibitied.” when trying to alter User Role and unable to Post via WP Admin
- Is Hfeed class for all pages needed
- Issue where WP Featured Image will not display
- Custom order for Mysql array
- Display post category in foreach loop
- Single.php – Get Current Parent Category
- insert a warning message into post-new.php
- WP text editor change html markup
- How to make different custom post layouts?
- Modifying the following code to displaying the title and the content of static page Posts page?
- Saving Post Data in Another Database
- How can I tell if a post has been published at least once?
- Add simple field column to the posts screen
- Regarding Posts
- Permanently deleted posts still exist
- WP_Query outputs wrong post in custom post type
- Order by meta_key field in WordPress not meta_value field value
- Deleted page still shows and is showing posts archive
- Blog featured images disappeared on my homepage
- Creating a frontend blog post form for logged in users only
- How to create frontend Post filter using meta query
- How to test if user is filtering post list in dashboard
- Post Format problem
- Post title to show all characters after having it limited for preview
- Some comment box wordpress in 1 post [closed]
- wordpress paginate, link to the right page
- How to get posts from network blog
- Get current user array with post string
- Rewrite a specific Post Format to URL slug
- Remove read more
- 1 post, 2 templates
- the_content() not displaying the full post
- Taxonomy Category category.php not working
- Insert Shortcode exactly at the end of the content
- Print specific content [closed]
- How to make ‘show_option_all’, that comes from wp_list_categoreis, to work with get_categories or with get_terms?
- Get related posts matching most of the provided tags using WP_Query