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?
- How to manually delete post from database without causing conflicts?
- Hide or remove custom post status
- Template for displaying CPT / Taxonomy URL issues
- Countrywise Post View
- Multiple posts in one page
- post__in no longer works in wordpress 5.0.3
- Scrollbar disappear in post window
- long-title posts do not want published
- call to jquery ajax failing with 500 error when passing extra data field
- How to count posts in tags within time limit
- Automatically select template based on query string for New Posts
- How to create a button to filter posts (list) by meta value
- Put page on Archived statut after end of publication date
- Moved a website and excerpts don’t work for old posts
- How to remove a post from results by ID after query?
- get_next_post() not working with future post status
- Why do I have categories with duplicate slugs?
- Blog featured images disappeared on my homepage
- Posting or Updating Article Fails on WordPress [closed]
- Blog images not showing on homepage
- How to display related post from same category in single.php
- Exclude posts in home slider from sections
- WordPress weird behaviour on comments
- post thumbnails error
- WordPress the_category(); only works with message-posts not with project posts, how do I specify project categories?
- Display images of related posts
- Display Updated Date Instead of Published Date
- Center crop the feature image
- Submit New Post
- Disaply products from category
- The letter “i” appears capitalised even when I don’t mean to capitalise them
- Multisite Issue on Subdomain’s article pages
- Comments are in database, don’t show up in WordPress backend or frontend
- I have two post types in the same query, how can I use CSS to select only one post type?
- Anchor links turn to p nbsp p
- Display all tags even if they are not assigned to a post
- Scheduled posts appear as clickable in the front end
- No comments allowed
- Many buttons not functioning in WordPress dashboard (as admin)
- Extracting the permalink from a post instance
- Is there a way to save different data when USER interacts with the same POST?
- How to add a block to a category page?
- Get post by two meta key but order by one of them
- how to make special modal for each post
- why my wordpress post, created programmatically, not opening detail page & redirecting to index instead
- How can I set the permalink of an article / page equal to the permalink of an existing article / page?
- New Post bubble
- Administrator Posts
- How do you update post date (year only) in a separate custom field?
- Sync All Post Type Tag
- Sorting post by custom field and category
- Post data across WordPress sites
- how to put post customized in page
- post and page and custom post type