Create post filter in custom wp-query
I'm trying to create a filter bar for my post feed on WordPress.
I'm trying to achieve this using the $_GET
method where I change Wp-query based on the parameters present in the URL.
For some reason it's not working for all the cases; note that the parameters are
- Year
- Month
- Category
- Keywords
And this is my wp-query code:
$current_url = $_SERVER['REQUEST_URI'];
$count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ( !isset($_GET['year']) && !isset($_GET['monthnum']) && !isset($_GET['category']) && !isset($_GET['string'])){
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Press Release', 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) || isset($_GET['monthnum']) || isset($_GET['category']) || isset($_GET['string']) ){
if( isset($_GET['year']) ){
$year = $_GET['year'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) ){
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) ){
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['string']) ){
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['string']) ){
$year = $_GET['year'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['category']) ){
$monthnum = $_GET['monthnum'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['string']) ){
$monthnum = $_GET['monthnum'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) && isset($_GET['string']) ){
$string = $_GET['string'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
}
php wordpress
|
show 5 more comments
I'm trying to create a filter bar for my post feed on WordPress.
I'm trying to achieve this using the $_GET
method where I change Wp-query based on the parameters present in the URL.
For some reason it's not working for all the cases; note that the parameters are
- Year
- Month
- Category
- Keywords
And this is my wp-query code:
$current_url = $_SERVER['REQUEST_URI'];
$count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ( !isset($_GET['year']) && !isset($_GET['monthnum']) && !isset($_GET['category']) && !isset($_GET['string'])){
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Press Release', 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) || isset($_GET['monthnum']) || isset($_GET['category']) || isset($_GET['string']) ){
if( isset($_GET['year']) ){
$year = $_GET['year'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) ){
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) ){
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['string']) ){
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['string']) ){
$year = $_GET['year'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['category']) ){
$monthnum = $_GET['monthnum'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['string']) ){
$monthnum = $_GET['monthnum'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) && isset($_GET['string']) ){
$string = $_GET['string'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
}
php wordpress
“For some reason it's not working for all the cases” - so for which isn’t it working, and what exactly does “not working” mean?
– misorude
Nov 15 '18 at 10:22
The structure with so many ifs is not really ideal. You should rather create an array for the parameters, and set the default values in there. Then, if$_GET['year']
is set, you overwrite the according value in the array, and same for all the other criteria. And in the end, you feed that array tonew WP_Query(…)
. That way, you don’t have to handle all the possible combinations separately.
– misorude
Nov 15 '18 at 10:25
Alright, the string one isn't working "Keywords filter". And what I mean is that in case I play around with the URL using the $_GET parameters I mentioned above; the results aren't filtered, I still get everything.
– Robz
Nov 15 '18 at 10:25
By keywords, you mean the parameter that is actually namedstring
? I can’t find_name__like
documented anywhere here, codex.wordpress.org/Class_Reference/WP_Query, so where did you get that from?
– misorude
Nov 15 '18 at 10:27
I tried this before, I created a less expended version but it didnt work I couldnt find a value to give the parameters so that they stop affecting the search in case they are not set yet. Lets say something like 'if( !isset($year)){$year = any}' so that it takes all the years.
– Robz
Nov 15 '18 at 10:29
|
show 5 more comments
I'm trying to create a filter bar for my post feed on WordPress.
I'm trying to achieve this using the $_GET
method where I change Wp-query based on the parameters present in the URL.
For some reason it's not working for all the cases; note that the parameters are
- Year
- Month
- Category
- Keywords
And this is my wp-query code:
$current_url = $_SERVER['REQUEST_URI'];
$count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ( !isset($_GET['year']) && !isset($_GET['monthnum']) && !isset($_GET['category']) && !isset($_GET['string'])){
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Press Release', 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) || isset($_GET['monthnum']) || isset($_GET['category']) || isset($_GET['string']) ){
if( isset($_GET['year']) ){
$year = $_GET['year'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) ){
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) ){
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['string']) ){
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['string']) ){
$year = $_GET['year'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['category']) ){
$monthnum = $_GET['monthnum'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['string']) ){
$monthnum = $_GET['monthnum'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) && isset($_GET['string']) ){
$string = $_GET['string'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
}
php wordpress
I'm trying to create a filter bar for my post feed on WordPress.
I'm trying to achieve this using the $_GET
method where I change Wp-query based on the parameters present in the URL.
For some reason it's not working for all the cases; note that the parameters are
- Year
- Month
- Category
- Keywords
And this is my wp-query code:
$current_url = $_SERVER['REQUEST_URI'];
$count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if ( !isset($_GET['year']) && !isset($_GET['monthnum']) && !isset($_GET['category']) && !isset($_GET['string'])){
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Press Release', 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) || isset($_GET['monthnum']) || isset($_GET['category']) || isset($_GET['string']) ){
if( isset($_GET['year']) ){
$year = $_GET['year'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) ){
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) ){
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['string']) ){
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$monthnum = $_GET['monthnum'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['monthnum']) ){
$year = $_GET['year'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year,'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['year']) && isset($_GET['string']) ){
$year = $_GET['year'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'year' => $year, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['category']) ){
$monthnum = $_GET['monthnum'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['monthnum']) && isset($_GET['string']) ){
$monthnum = $_GET['monthnum'];
$string = $_GET['string'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'monthnum' => $monthnum, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
if( isset($_GET['category']) && isset($_GET['string']) ){
$string = $_GET['string'];
$category = $_GET['category'];
$wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
}
}
php wordpress
php wordpress
asked Nov 15 '18 at 10:12
RobzRobz
155
155
“For some reason it's not working for all the cases” - so for which isn’t it working, and what exactly does “not working” mean?
– misorude
Nov 15 '18 at 10:22
The structure with so many ifs is not really ideal. You should rather create an array for the parameters, and set the default values in there. Then, if$_GET['year']
is set, you overwrite the according value in the array, and same for all the other criteria. And in the end, you feed that array tonew WP_Query(…)
. That way, you don’t have to handle all the possible combinations separately.
– misorude
Nov 15 '18 at 10:25
Alright, the string one isn't working "Keywords filter". And what I mean is that in case I play around with the URL using the $_GET parameters I mentioned above; the results aren't filtered, I still get everything.
– Robz
Nov 15 '18 at 10:25
By keywords, you mean the parameter that is actually namedstring
? I can’t find_name__like
documented anywhere here, codex.wordpress.org/Class_Reference/WP_Query, so where did you get that from?
– misorude
Nov 15 '18 at 10:27
I tried this before, I created a less expended version but it didnt work I couldnt find a value to give the parameters so that they stop affecting the search in case they are not set yet. Lets say something like 'if( !isset($year)){$year = any}' so that it takes all the years.
– Robz
Nov 15 '18 at 10:29
|
show 5 more comments
“For some reason it's not working for all the cases” - so for which isn’t it working, and what exactly does “not working” mean?
– misorude
Nov 15 '18 at 10:22
The structure with so many ifs is not really ideal. You should rather create an array for the parameters, and set the default values in there. Then, if$_GET['year']
is set, you overwrite the according value in the array, and same for all the other criteria. And in the end, you feed that array tonew WP_Query(…)
. That way, you don’t have to handle all the possible combinations separately.
– misorude
Nov 15 '18 at 10:25
Alright, the string one isn't working "Keywords filter". And what I mean is that in case I play around with the URL using the $_GET parameters I mentioned above; the results aren't filtered, I still get everything.
– Robz
Nov 15 '18 at 10:25
By keywords, you mean the parameter that is actually namedstring
? I can’t find_name__like
documented anywhere here, codex.wordpress.org/Class_Reference/WP_Query, so where did you get that from?
– misorude
Nov 15 '18 at 10:27
I tried this before, I created a less expended version but it didnt work I couldnt find a value to give the parameters so that they stop affecting the search in case they are not set yet. Lets say something like 'if( !isset($year)){$year = any}' so that it takes all the years.
– Robz
Nov 15 '18 at 10:29
“For some reason it's not working for all the cases” - so for which isn’t it working, and what exactly does “not working” mean?
– misorude
Nov 15 '18 at 10:22
“For some reason it's not working for all the cases” - so for which isn’t it working, and what exactly does “not working” mean?
– misorude
Nov 15 '18 at 10:22
The structure with so many ifs is not really ideal. You should rather create an array for the parameters, and set the default values in there. Then, if
$_GET['year']
is set, you overwrite the according value in the array, and same for all the other criteria. And in the end, you feed that array to new WP_Query(…)
. That way, you don’t have to handle all the possible combinations separately.– misorude
Nov 15 '18 at 10:25
The structure with so many ifs is not really ideal. You should rather create an array for the parameters, and set the default values in there. Then, if
$_GET['year']
is set, you overwrite the according value in the array, and same for all the other criteria. And in the end, you feed that array to new WP_Query(…)
. That way, you don’t have to handle all the possible combinations separately.– misorude
Nov 15 '18 at 10:25
Alright, the string one isn't working "Keywords filter". And what I mean is that in case I play around with the URL using the $_GET parameters I mentioned above; the results aren't filtered, I still get everything.
– Robz
Nov 15 '18 at 10:25
Alright, the string one isn't working "Keywords filter". And what I mean is that in case I play around with the URL using the $_GET parameters I mentioned above; the results aren't filtered, I still get everything.
– Robz
Nov 15 '18 at 10:25
By keywords, you mean the parameter that is actually named
string
? I can’t find _name__like
documented anywhere here, codex.wordpress.org/Class_Reference/WP_Query, so where did you get that from?– misorude
Nov 15 '18 at 10:27
By keywords, you mean the parameter that is actually named
string
? I can’t find _name__like
documented anywhere here, codex.wordpress.org/Class_Reference/WP_Query, so where did you get that from?– misorude
Nov 15 '18 at 10:27
I tried this before, I created a less expended version but it didnt work I couldnt find a value to give the parameters so that they stop affecting the search in case they are not set yet. Lets say something like 'if( !isset($year)){$year = any}' so that it takes all the years.
– Robz
Nov 15 '18 at 10:29
I tried this before, I created a less expended version but it didnt work I couldnt find a value to give the parameters so that they stop affecting the search in case they are not set yet. Lets say something like 'if( !isset($year)){$year = any}' so that it takes all the years.
– Robz
Nov 15 '18 at 10:29
|
show 5 more comments
0
active
oldest
votes
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%2f53317047%2fcreate-post-filter-in-custom-wp-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53317047%2fcreate-post-filter-in-custom-wp-query%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
“For some reason it's not working for all the cases” - so for which isn’t it working, and what exactly does “not working” mean?
– misorude
Nov 15 '18 at 10:22
The structure with so many ifs is not really ideal. You should rather create an array for the parameters, and set the default values in there. Then, if
$_GET['year']
is set, you overwrite the according value in the array, and same for all the other criteria. And in the end, you feed that array tonew WP_Query(…)
. That way, you don’t have to handle all the possible combinations separately.– misorude
Nov 15 '18 at 10:25
Alright, the string one isn't working "Keywords filter". And what I mean is that in case I play around with the URL using the $_GET parameters I mentioned above; the results aren't filtered, I still get everything.
– Robz
Nov 15 '18 at 10:25
By keywords, you mean the parameter that is actually named
string
? I can’t find_name__like
documented anywhere here, codex.wordpress.org/Class_Reference/WP_Query, so where did you get that from?– misorude
Nov 15 '18 at 10:27
I tried this before, I created a less expended version but it didnt work I couldnt find a value to give the parameters so that they stop affecting the search in case they are not set yet. Lets say something like 'if( !isset($year)){$year = any}' so that it takes all the years.
– Robz
Nov 15 '18 at 10:29