diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-10-19 10:04:14 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-10-19 10:04:14 +0200 |
commit | ac93ed4d29dd85409fb4c0cd9c2af266e90777c1 (patch) | |
tree | 6a0b4487439bfe068d4b7d412be70d4f90faa2a5 /WP/search-by-title-with-wp-query.php |
Diffstat (limited to 'WP/search-by-title-with-wp-query.php')
-rw-r--r-- | WP/search-by-title-with-wp-query.php | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/WP/search-by-title-with-wp-query.php b/WP/search-by-title-with-wp-query.php new file mode 100644 index 0000000..418f057 --- /dev/null +++ b/WP/search-by-title-with-wp-query.php @@ -0,0 +1,11 @@ +<?php + +// Add posts_where filter for title_like +add_filter('posts_where', function ($where, \WP_Query $wp_query) { + if ($title = $wp_query->get('title_like')) { + global $wpdb; + $where .= " AND {$wpdb->posts}.post_title LIKE '" . esc_sql($wpdb->esc_like($title)) . "'"; + } + + return $where; +}, 10, 2); |