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
- 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
- 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
- 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
- Removing the first image in content
- Check if row exists before inserting
- wpdb_prepare with multiple or condition
- exclude a category from a search on a specific page
- How to correctly pass values to wpdb->prepare()?
- What’s wrong with my $wpdb prepare?
- example of build_query function?
- Why is variable not working on custom sql query using wpdb?
- Get stock by custom meta field on all Woocommerce variable products
- Shortcode pagination not advancing
- Display fields as values in array from external SQL DB
- WSoD being caused by this piece of code
- Advanced SELECT query with condtional statements
- How do I see the mysql query generated by get_posts( $args )?
- Create table from array with prepare
- $wpdb->insert() does not Insert record in a table
- wpdb query problem to access previous 3 days posts
- WordPress SQL Issue not returning correct reselts
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Making a query to the DB using same parameters of loop
- Connecting to a 2nd DB gives me no results when querying
- Cannot get wpdb data (Error in a simple fuction) [closed]
- How to ignore posts in pre_get_posts?
- wpdb COALESCE won’t work
- Make a SQL query with wpdb in WordPress
- Custom Query – Based on user input
- 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 use mysql LIKE with wpdb?
- PHP -> SQL Query with Summing
- Query Column of Specific ID from Database Table
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- $wpdb->prepare affecting the query?
- 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
- mySQL queries are executed twice on wordpress website
- Get the alphabetical sorted data with 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?
- 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?
- 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
- 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?
- $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