I figured it out! Pretty simple actually.. You can replace post_date_gmt with ID depending on your needs. post_author != 1 will prevent the admin’s post from being deleted, so the menu links and pages will stay intact 🙂
function deleteOldestPost(){
global $wpdb;
$prefix = $wpdb->prefix;
$wpdb->query("DELETE
FROM ".$prefix."posts where post_author != 1 order by post_date_gmt asc limit 1");
}
add_action('publish_post', 'deleteOldestPost');
There will be some transient data, i.e. wp_term_relationships. Any of the database optimize plugins will get rid of them.
Related Posts:
- Function returning queried meta value based on current post ID
- Multipart/formatted MySQL query problem
- Is it good practice to use wpdb->query() function?
- Is there a WP function to get taxonomy name from taxonomy_term_id?
- Writing a function for WP Cron to run a SQL command daily
- sort title descending with title with number
- Remove posts that start with similar words (like a delete duplicate posts plugin)
- How do you properly prepare a %LIKE% SQL statement?
- What SQL Query to do a simple find and replace
- Return only Count from a wp_query request?
- Is there a way to list all used/unused WP templates?
- Order posts (across the whole site) by metadata date
- Sort posts by Date (DESC) and by Title (ASC)
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Filter the query ONLY for the search results page
- wpdb->prepare function remove single quote for %s in SQL statment
- Inserting Post Meta From SQL
- How to make an activities stream mixing posts and comments?
- mysql custom wp query
- SQL query equivalent to WP User Query
- Delete post revisions on post publish
- Are database queries created using WordPress filters protected from SQL injection?
- How to execute mulitple statement SQL queries using $wpdb->query?
- Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
- my function doesn’t return my post from today
- Query WordPress database by registered date and role
- How to get last updated row or ID in wordpress
- Custom Query – Based on user input
- Custom WordPress Table wpdb
- Best practice to limit results in get_row()?
- Placing a widget with $wpdb query
- Use $wpdb or other PHP script method to find/replace in WP database
- How can I add a new row in a separate database when someone registers via WordPress?
- Get posts from category with custom query
- How to get INSERT errors from $wpdb?
- how to insert missing tags into the posts through mySQL?
- How to use mysql LIKE with wpdb?
- PHP -> SQL Query with Summing
- SQL query to set posts in bulk based on the post content
- Query Column of Specific ID from Database Table
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- get_terms (or tax_query) for term of current post?
- $wpdb->prepare affecting the query?
- prepare function sql safe method
- wp_usermeta – Read from database, but save function broken
- Updating with $qpdb->query() always returns 0 rows affected
- Why my query does not run with prepare
- WordPress Insert not working with ajax
- shorthand syntax for custom fields
- mySQL queries are executed twice on wordpress website
- Get the alphabetical sorted data with wpdb
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- WordPress Mysql query and Duplicate
- making php value numeric
- How do I query for posts by custom meta and those that have been stickied?
- Help with Related Posts Function
- WordPress Categories: Function using custom SQL to return array of specific category IDs
- SQL error with custom query
- How to avoid saving empty data to sql while using add_meta_box
- This code is supposed to only allow user to be authenticated if accountVerified is equal to 1, but it still allows user to be authenticated otherwise
- Using One Function To Create Two Different Shortcodes
- Database SQL query error
- How can I connect to a second database and still be able to use wp functions like get_post_types()?
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Customising the default wordpress search functionality
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Selecting specific query for offset
- Why isn’t comment validation working?
- SQL query to delete users with multiple meta keys and comments
- WordPress Database Query works in phpMyAdmin but not in the code
- Shortcode with pagination advancing multiple queries
- WPDB delivers wrong results from complex queries
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- $wpdb post type and term query only works when there are no dashes or spaces in the term slug and title
- Fastest and most efficient SQL query to check if UID exists
- What argument does my function need to echo get_results() query results
- How can I display a query in a page?
- What to do to have access to WordPress functions
- How to make MySQL search queries with quotes
- $wpdb query for price in custom field value
- Database SQL Error, Should Work
- Database Query not working
- Run an update query in a function
- How to add next height number in Order Attributes inside the Add new page. [duplicate]
- Wpdb generates too many queries
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Mysql query and odd results
- Query is not work
- Sorting date results from a query
- How to Delete Posts by title?
- Get all the related data from WordPress DB
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- WPDB query suddenly not working
- Include posts under a custom taxonomy in author.php
- How to cache results of of get_available_post_statuses() function as it is causing a slow query on a website with over 1 million posts?
- How to properly prepare a column name if passed to a stored procedure?
- SQL errors when querying for something with apostrophes
- Function wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder