You should be able to get away with just adding AND $wpdb->posts.post_status="publish"
at the end.
$prepare_string = "
SELECT DISTINCT ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
LEFT JOIN $wpdb->postmeta AS mt1 ON ( $wpdb->posts.ID = mt1.post_id )
WHERE ( ( $wpdb->postmeta.meta_key = 'event_date' AND $wpdb->postmeta.meta_value >= %d )
OR ( ( $wpdb->postmeta.meta_key = 'event_date' AND $wpdb->postmeta.meta_value < %d )
AND ( mt1.meta_key = 'end_date' AND mt1.meta_value >= %d ) )
OR ( $wpdb->postmeta.meta_key = 'event_date' AND $wpdb->postmeta.meta_value="" ) )
AND $wpdb->posts.post_status="publish"
";
Related Posts:
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- How to use mysqli_query() in PHP?
- Access Denied for User ‘root’@’localhost’ (using password: YES) – No Privileges?
- MySQL Error “Operand should contain 1 column”
- How to use MySQL DECIMAL?
- MySQL ERROR 1290 (HY000) –secure-file-priv option
- MySQL server startup error ‘The server quit without updating PID file’
- What does SQL Select symbol || mean?
- PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused
- Mysql: Setup the format of DATETIME to ‘DD-MM-YYYY HH:MM:SS’ when creating a table
- Duplicating a MySQL table, indices, and data
- mysql Foreign key constraint is incorrectly formed error
- Access Denied for User ‘root’@’localhost’ (using password: YES) – No Privileges?
- ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111)
- SELECT * FROM multiple tables. MySQL
- mysql: SOURCE error 2?
- java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
- How to avoid MySQL ‘Deadlock found when trying to get lock; try restarting transaction’
- ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
- Data truncated for column?
- MySQL foreach loop
- How to find MySQL process list and to kill those processes?
- MySQL IF NOT NULL, then display 1, else display 0
- MySQL Calculate Percentage
- Repair all tables in one go
- How can I output MySQL query results in CSV format?
- Error: select command denied to user ‘
‘@’ ‘ for table ‘ ‘ - Get the new record primary key ID from MySQL insert query?
- How to change collation of database, table, column?
- Failed building wheel for mysql-python
- MySQL Error 1215: Cannot add foreign key constraint
- ERROR 1115 (42000): Unknown character set: ‘utf8mb4’
- Error Code: 1290. The MySQL server is running with the –secure-file-priv option so it cannot execute this statement
- Remove Primary Key in MySQL
- How to get the max of two values in MySQL?
- mysql: connection refused when trying to connect to localhost using remote IP
- Error: Duplicate entry ‘0’ for key ‘PRIMARY’
- Why are simple updates to wp_postmeta’s “_edit_lock” so slow?
- Complex meta query with 3 keys
- How can I find the source of slow queries in WordPress?
- How to log mysql errors from wordpress core?
- $wpdb and MySQL Create Trigger
- wpdb Custom Meta Data with 2 conditions
- query to create woocommerce products from images
- Error: MySQL shutdown unexpectedly. XAMPP local conf running WordPress
- Configure WordPress to connect to Mysql DB using SSH tunneling
- How can I make wordpress suppress mysql errors?
- How to bulk delete all WordPress subscribers?
- Error while importing database
- Changing Table Prefix for an Existing WordPresss Site
- Set meta_key and meta_value for all registered user in wordpress using sql query [closed]
- Mysql SELECT posts
- Connect another DB and fetch records from some tables
- Can I restart MySQL engine when WordPress website is live?
- Error: MySQL shutdown unexpectedly [closed]
- #1115 – Unknown character set: ‘utf8mb4’
- Why my wordpress loopback traffic while socket is setup?
- How to add a private user page populated from sql?
- Update or insert meta key value – On Duplicate key update
- Connect to MySQL using Windows Authentication
- Defining the same price to all WooCommerce Products
- Database Structure on Different Servers
- SQL syntax error. However, it works normally at phpmyadmin
- How to get INSERT errors from $wpdb?
- How to set MYSQLI_OPT_INT_AND_FLOAT_NATIVE option
- string literals in query
- Export Yoast Keywords from MySQL/phpMyAdmin
- Same database; different WordPress
- Create pages from MySQL database
- How to insert dash (-) into database using wpdb and new_to_publish hook?
- MySQL: How to change url in posts from xxx.com to yyy.org?
- How to fetch all meta_key and meta_value using post_id wihout duplication of post meta using sql query
- Exporting completed webiste for another server
- Upgrade DB Loop – WordPress DB Version Conflict
- WordPress & MySQL , problem with use many array in meta_query
- Button writing to MYSQL database
- mysqli_sql_exception: Unknown column ‘wp_’ in ‘field list’ PHP 8
- MySQL ‘duplicate entry’ error
- Find locations of all featured images of draft posts via SQL
- WordPress Dutch characters encoding problem
- How to show result of sql query in a page?
- Why can’t i come back to older working version of php after ugrade with error log on php 7.2 and 7.3
- How do I insert content of various MySQL fields in post?
- How to change link in all posts?
- Select latest 2 articles, but only from given category
- CONCAT_WS in custom sql query
- Query does not filter duplicate _sku numbers
- Ordering topics by most replies in X days using MYSQL statement
- MySQL Select within WP Page template
- wp_postmeta – lot of meta fields
- Running WordPress from MySQL Cluster with HAPRoxy
- Delete posts with word count less than x number of words
- MySQL: get post_tag items that are tags (not SEO keywords)
- $wpdb->get_var returns 0
- cannot log in to the mysql server (wamp and wordpress)
- SQL command to export post_content from wp_posts using phpMyAdmin
- Why does the MySQL command line tool ignore the –port parameter?
- Postgres equivalent to MySQL’s \G?
- Changing host permissions for MySQL users
- Possible SQL injection. How to locate and fix?