There are 2 places that could have issues:
- Your first
wpdb::prepare()
here:
// Prepare and execute the count query
$count_query = $wpdb->prepare( $count_sql, ...$query_params );
The initial value you set for $count_sql
is "SELECT COUNT(*) FROM $table_name WHERE 1=1"
, and if $filters
doesn’t have any key matching your switch
, the value of $count_sql
will remain as its initial value, and as you can see, there are no placeholders in your initial value, which leads the the error. You can avoid it by giving default placeholder(s) in $count_sql
, and default value(s) in $query_params
.
- The following code (your last
wpdb::prepare()
) definitely leads to error:
// Prepare and execute the paginated query
$query = $wpdb->prepare( $paginated_sql, ...$query_params );
The reason for this is that you have this query for $paginated_sql
:
// Add LIMIT and OFFSET to the SQL query
$paginated_sql = $base_sql . $wpdb->prepare( " LIMIT %d OFFSET %d", $per_page, $offset );
Since you already use $wpdb->prepare()
for $paginated_sql
, all of its placeholders have been updated with the given values. As a result, there are no placeholders in $paginated_sql
anymore, leading to the error when you run $wpdb->prepare()
again.