[wp-trac] [WordPress Trac] #30982: Meta query in pre_get_posts
WordPress Trac
noreply at wordpress.org
Sun Jan 11 20:36:45 UTC 2015
#30982: Meta query in pre_get_posts
-----------------------------------+-----------------------------
Reporter: Spidlace | Owner:
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: 4.1
Severity: normal | Keywords:
Focuses: template, performance |
-----------------------------------+-----------------------------
I don't know if it is already in place, but I want to add in the hook
"pre_get_posts" custom field. In the page "search.php", I want to search
"example" in post_title, content and custom field in all pages, posts and
post-type.
But this is the code I put in the hook :
{{{
function recherche_avancee( $query ) {
if ( !is_admin() && $query->is_search ) {
$custom_fields = array(
"introduction",
"ensavoirplus"
);
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $_GET['s'],
'compare' => 'LIKE'
));
}
$query->set("meta_query", $meta_query);
}
}
add_action( 'pre_get_posts', 'recherche_avancee');
}}}
and this is the SQL code I get :
{{{
1. SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
2. FROM wp_posts
3. INNER JOIN wp_postmeta
4. ON ( wp_posts.ID = wp_postmeta.post_id )
5. WHERE 1=1
6. AND (((wp_posts.post_title LIKE '%exemple%')
7. OR (wp_posts.post_content LIKE '%exemple%')))
8. AND wp_posts.post_type IN ('post', 'page', 'attachment')
9. AND (wp_posts.post_status = 'publish'
10. OR wp_posts.post_status = 'miseenavant'
11. OR wp_posts.post_author = 3)
13. AND ( ( wp_postmeta.meta_key = 'introduction'
14. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%exemple%' )
15. OR ( wp_postmeta.meta_key = 'en_savoir_plus_page'
16. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%exemple%' ) )
17. GROUP BY wp_posts.ID
18. ORDER BY wp_posts.menu_order ASC
19. LIMIT 0, 10
}}}
I want to add the line 13, 14, 15, 16 on the top on the code after the
search in the content and the title. And I don't want a '''AND''' before
the line 13 but a '''OR'''.
But here's the SQL I would need :
{{{
1. SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
2. FROM wp_posts
3. INNER JOIN wp_postmeta
4. ON ( wp_posts.ID = wp_postmeta.post_id )
5. WHERE 1=1
6. AND (((wp_posts.post_title LIKE '%exemple%')
7. OR (wp_posts.post_content LIKE '%exemple%')
8. OR ( wp_postmeta.meta_key = 'introduction'
9. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%exemple%' )
10. OR ( wp_postmeta.meta_key = 'en_savoir_plus_page'
11. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%exemple%' ))
12. AND wp_posts.post_type IN ('post', 'page', 'attachment')
13. AND (wp_posts.post_status = 'publish'
14. OR wp_posts.post_status = 'miseenavant'
15. OR wp_posts.post_author = 3)
16. GROUP BY wp_posts.ID
17. ORDER BY wp_posts.menu_order ASC
18. LIMIT 0, 10
}}}
Is it possible to do this now? If this is not the case, is it possible to
consider it in the "future"?
Hope this is not already proposed ...
Thank you for reading :-)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/30982>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list