WP: custom search
I really can't figure this out and it drives me crazy. I added my own custom search function in functions.php to be able to search in post meta.
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
var_dump($query->get('meta_query'));
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
The var_dump gives me this result:
array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(18) "Korte beschrijving" ["compare"]=> string(4) "LIKE" } }
so I assume that the meta_query is correct. This is the full query:
object(WP_Query)#754 (46) { ["query"]=> array(1) { ["s"]=> string(12) "beschrijving" } ["query_vars"]=> array(53) { ["s"]=> string(12) "beschrijving" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(12) "beschrijving" ["compare"]=> string(4) "LIKE" } } } ["tax_query"]=> object(WP_Tax_Query)#3628 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> NULL ["primary_id_column"]=> NULL } ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(true) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "bf0637eea0229d81220060e8015682ce" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }
I don't get any results while I have at least one post that contains the value I'm looking for. What's wrong with this code? Please help since I'm trying to make work for some time but not able to.
php wordpress search
add a comment |
I really can't figure this out and it drives me crazy. I added my own custom search function in functions.php to be able to search in post meta.
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
var_dump($query->get('meta_query'));
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
The var_dump gives me this result:
array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(18) "Korte beschrijving" ["compare"]=> string(4) "LIKE" } }
so I assume that the meta_query is correct. This is the full query:
object(WP_Query)#754 (46) { ["query"]=> array(1) { ["s"]=> string(12) "beschrijving" } ["query_vars"]=> array(53) { ["s"]=> string(12) "beschrijving" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(12) "beschrijving" ["compare"]=> string(4) "LIKE" } } } ["tax_query"]=> object(WP_Tax_Query)#3628 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> NULL ["primary_id_column"]=> NULL } ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(true) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "bf0637eea0229d81220060e8015682ce" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }
I don't get any results while I have at least one post that contains the value I'm looking for. What's wrong with this code? Please help since I'm trying to make work for some time but not able to.
php wordpress search
add a comment |
I really can't figure this out and it drives me crazy. I added my own custom search function in functions.php to be able to search in post meta.
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
var_dump($query->get('meta_query'));
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
The var_dump gives me this result:
array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(18) "Korte beschrijving" ["compare"]=> string(4) "LIKE" } }
so I assume that the meta_query is correct. This is the full query:
object(WP_Query)#754 (46) { ["query"]=> array(1) { ["s"]=> string(12) "beschrijving" } ["query_vars"]=> array(53) { ["s"]=> string(12) "beschrijving" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(12) "beschrijving" ["compare"]=> string(4) "LIKE" } } } ["tax_query"]=> object(WP_Tax_Query)#3628 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> NULL ["primary_id_column"]=> NULL } ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(true) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "bf0637eea0229d81220060e8015682ce" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }
I don't get any results while I have at least one post that contains the value I'm looking for. What's wrong with this code? Please help since I'm trying to make work for some time but not able to.
php wordpress search
I really can't figure this out and it drives me crazy. I added my own custom search function in functions.php to be able to search in post meta.
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
var_dump($query->get('meta_query'));
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
The var_dump gives me this result:
array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(18) "Korte beschrijving" ["compare"]=> string(4) "LIKE" } }
so I assume that the meta_query is correct. This is the full query:
object(WP_Query)#754 (46) { ["query"]=> array(1) { ["s"]=> string(12) "beschrijving" } ["query_vars"]=> array(53) { ["s"]=> string(12) "beschrijving" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["meta_query"]=> array(1) { [0]=> array(3) { ["key"]=> string(16) "wpcf-description" ["value"]=> string(12) "beschrijving" ["compare"]=> string(4) "LIKE" } } } ["tax_query"]=> object(WP_Tax_Query)#3628 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> NULL ["primary_id_column"]=> NULL } ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(true) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> string(32) "bf0637eea0229d81220060e8015682ce" ["query_vars_changed":"WP_Query":private]=> bool(false) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }
I don't get any results while I have at least one post that contains the value I'm looking for. What's wrong with this code? Please help since I'm trying to make work for some time but not able to.
php wordpress search
php wordpress search
edited Nov 26 '18 at 16:29
Vadim Kotov
4,65663447
4,65663447
asked Nov 25 '18 at 11:23
HamzaHamza
1815
1815
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The issue is, you are attempting to query the meta values only on the posts that are initially returned from the search, which is none. I.E. your query looks for posts that have the search term in either the title/content and in your meta query.
Clear your search query after applying your custom meta query:
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search() ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->get('s'),
'compare' => 'LIKE'
)
));
// Clear the initial search query
$query->set('s', '');
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466953%2fwp-custom-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is, you are attempting to query the meta values only on the posts that are initially returned from the search, which is none. I.E. your query looks for posts that have the search term in either the title/content and in your meta query.
Clear your search query after applying your custom meta query:
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search() ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->get('s'),
'compare' => 'LIKE'
)
));
// Clear the initial search query
$query->set('s', '');
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
add a comment |
The issue is, you are attempting to query the meta values only on the posts that are initially returned from the search, which is none. I.E. your query looks for posts that have the search term in either the title/content and in your meta query.
Clear your search query after applying your custom meta query:
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search() ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->get('s'),
'compare' => 'LIKE'
)
));
// Clear the initial search query
$query->set('s', '');
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
add a comment |
The issue is, you are attempting to query the meta values only on the posts that are initially returned from the search, which is none. I.E. your query looks for posts that have the search term in either the title/content and in your meta query.
Clear your search query after applying your custom meta query:
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search() ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->get('s'),
'compare' => 'LIKE'
)
));
// Clear the initial search query
$query->set('s', '');
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
The issue is, you are attempting to query the meta values only on the posts that are initially returned from the search, which is none. I.E. your query looks for posts that have the search term in either the title/content and in your meta query.
Clear your search query after applying your custom meta query:
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search() ) {
echo 'Is search: true <br>';
$query->set('meta_query', array(
array(
'key' => 'wpcf-description',
'value' => $query->get('s'),
'compare' => 'LIKE'
)
));
// Clear the initial search query
$query->set('s', '');
}
}
add_filter( 'pre_get_posts', 'custom_search_query',9999);
edited Nov 26 '18 at 21:14
answered Nov 26 '18 at 16:39
GeorgeGeorge
31.1k74378
31.1k74378
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
add a comment |
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
THANK YOU BRO! This works like a charm
– Hamza
Nov 26 '18 at 20:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466953%2fwp-custom-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown