SQL query to retrieve the number of WordPress posts with all given categories

There is a working example that only counts posts that are associated with both categories. This is tested with 3 posts, not 5000. function count_posts_with_categories($cat_ids) { global $wpdb; $cat_count = 0; // A subquery to get all posts that are assigned to each category $subquery = “SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (” . … Read more

Change permalinks in posts via SQL

If you are using MySQL 8 and above you can use REGEXP_REPLACE, e.i. UPDATE `wp_posts` SET post_content=REGEXP_REPLACE(post_content, ‘domain.tld\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/’, ‘domain.tld/’) WHERE post_content REGEXP ‘domain.tld\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/’; this would look for exact match domain.tld/{4 digit int}/{2 digit int}/{2 digit int}/ then replace it with domain.tld/ so it will replace something like domain.tld/2000/01/01/my-cool-post/ to domain.tld/my-cool-post/ Another option if you cannot … Read more

Load posts via AJAX without draft status

admin-ajax.php is treated as part of the admin, so protected statuses will be included. To solve this just explicitly define post_status as publish to only get published posts: $args = array( ‘post_type’ => ‘project’, ‘post_status’ => ‘publish’ ); Or, better yet, consider using the REST API for AJAX requests, instead of the aging admin-ajax.php approach. … Read more