Created
December 11, 2013 13:41
-
-
Save nielsvr/7910618 to your computer and use it in GitHub Desktop.
Search trough all WordPress pages and ALL the custom meta values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
$keyword = get_search_query(); | |
$keyword = '%' . like_escape( $keyword ) . '%'; | |
// Search in all custom fields | |
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( " | |
SELECT DISTINCT post_id FROM {$wpdb->postmeta} | |
WHERE meta_value LIKE '%s' | |
", $keyword ) ); | |
// Search in post_title and post_content | |
$post_ids_post = $wpdb->get_col( $wpdb->prepare( " | |
SELECT DISTINCT ID FROM {$wpdb->posts} | |
WHERE post_title LIKE '%s' | |
OR post_content LIKE '%s' | |
", $keyword, $keyword ) ); | |
$post_ids = array_merge( $post_ids_meta, $post_ids_post ); | |
// Query arguments | |
$args = array( | |
'post_type' => 'page', | |
'post_status' => 'publish', | |
'post__in' => $post_ids, | |
'posts_per_page' => 20 | |
); | |
$search_query = new WP_Query( $args ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment