WordPress Custom Search On Custom field - php

I'm having troubles regarding my search. My search query runs fine, it gives the result as it should be, but the problem is it also creates a dummy product of the first searched product.
Like this image:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'property_add_location_property_add_country',
'value' => $_POST['property_add_location_property_add_country'],
'compare' => 'IN',
),
array(
'key' => 'property_add_location_property_add_state',
'value' => $_POST['property_add_location_property_add_state'],
'compare' => 'IN',
),
array(
'key' => 'property_add_location_property_add_city',
'value' => $_POST['property_add_location_property_add_city'],
'compare' => '=',
),
)));
And this is my code which I run on Archie-product.php.
Please help me out, I have been doing everything to solve this for over 4 days but I can't find a solution!

Related

WordPress query posts by ACF

UPDATE - SOLVED
Okay so I have three custom post types 'courses', 'modules' and 'results'.
These post types are set up with a number of ACF (Advanced Custom Fields).
I'm trying to query the 'results' CPT based on their ACF's using 'meta_query'.
The two ACF fields I'm trying to query with are 'module' and 'user'.
'module' - Set up as a 'Relationship' field type filtering the
'module' custom post type.
'user' - Set up as a (Relational) 'User' field type.
I seem to only get an empty array back. I've looked at the examples on https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field%20parameters still without any luck.
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => $module_id,
'compare' => '=',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
Call function:
print_r(check_results_for_user($post->ID, $currentUserID ));
Result:
Array ( )
UPDATE - Solved:
Okay so managed to figure it out. Was due to the 'module' relationship field saving it’s data as a serialized array: a:1:{i:0;s:1:"9";}
So you have to change the compare to 'LIKE' and wrap the value in "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
See the end of this page: https://www.advancedcustomfields.com/resources/querying-relationship-fields/
UPDATE - Solved:
Okay so managed to figure it out. Was due to the 'module' relationship field saving it’s data as a serialized array: a:1:{i:0;s:1:"9";}
So you have to change the compare to 'LIKE' and wrap the value in "
function check_results_for_user( $module_id, $user_id ){
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'results',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'module',
'value' => '"'.$module_id.'"',
'compare' => 'LIKE',
),
array(
'key' => 'user',
'value' => $user_id,
'compare' => '=',
),
),
));
return $posts;
}
See the end of this page: https://www.advancedcustomfields.com/resources/querying-relationship-fields/

Wordpress - Multiple Meta Keys - Order by rank

Hello I have something like this
if(isset($_POST["select_1"]) or isset($_POST["select_2"])){
$args = array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $meta_key_list1,
'value' => $meta_key_val,
'compare' => '='
),
array(
'key' => $meta_key_list2,
'value' => $meta_key_val,
'compare' => '='
),
array(
'key' => $meta_key_list3,
'value' => $meta_key_val,
'compare' => '='
),
),
);
}else{
$args = array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'page_rank'
);
}
I need show all pages which have set custom field "page_rank" and order it DESC. "Else" working good but first part not working. I try something like in "else" but this not working for multiple key.
Please do you know anyone how to solve this problem?
Thank you !
EDIT:
Solved I changed OR to AND thank you for help
Try 'compare' => 'LIKE' instead of 'compare' => '=' and verify your keys and values in the meta query

Wordpress meta_query OR/AND relations not working

I have a search form with multiple options that are taxonomies. When I select all options, or just one, and click search it never returns any record. I have a problem with understanding relations, and how to properly set arrays.
$args = array('post_type' => array( 'property' ),
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'property_address_postal_code',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_address_state',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_location',
'value' => $search,
'compare' => 'LIKE',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
)
)
);
First off, I'm not sure if you're quite understanding how the relation property applies in this function. It applies AND/OR to other properties on the same level. So your very first relation => AND makes sure your $search variable matches one of the properties you specify AND that the property_price is between the min and max values.
Secondly, with subsequent, nested meta queries, the default relation is AND, so when you try to match $search against different properties, you're actually trying to make sure that $search is matching ALL of the properties you specify. You can fix that by trying to match $search against one property OR another property.
Thirdly, with your comparison of the price against the min and max price, the meta query is malformed, and both of those meta queries are identical. In that case, you don't need a relation since you only have to check against one value.
You can simplify your code as such:
$args = array('post_type' => array( 'property' ),
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'property_address_postal_code',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_address_state',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_location',
'value' => $search,
'compare' => 'LIKE',
),
),
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
)
);
Now, you're making a query that checks if $search is like property_address_potal_code OR like property_address_state OR like property_location AND property_price is between the min and max price.
You might still have to fiddle with it a bit depending on other logic in your code, but I think this should get you going in the right direction.

Don't display WordPress posts if time has passed using WP_Query

I have a custom post type of schedule which I can add 'events' with a custom field of Timeslot. This custom field is a repeater using ACF Pro.
It contains a date, start time and end time. Date picker and time pickers respectively.
On the homepage I have an image slider that pulls in the artwork and event title and only display 'todays' events, in time order.
What I want to do now is to not show events that have passed. So if the current time is 14:00pm for example, the all shows that ended before that time will not show. If the show started at 13:00pm and ends at 15:00pm though, that show will still show, and will appear first. I hope that makes sense?!
Here is my WP_Query so far but after a few attempts, I cannot figure out how to modify it to work as required:
<?php
$args = array(
'post_type' => 'schedule',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_date',
'compare' => '=',
'value' => $day,
),
'time_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_start_time',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
<?php
$args = array(
'post_type' => 'schedule',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_date',
'compare' => '=',
'value' => $day,
),
'time_clause' => array(
'key' => 'schedule_time_slot_%_schedule_show_start_time',
'compare' => '>=',
'value' => time(),
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
)
);
Note the change in the time_clause argument. So only show time_clause when it equals or is later than the time now.

Wordpress post query on more than one condition

I have the following function which returns only future events, which works great:
$args = array(
'post_type' => self::POST_TYPE,
'posts_per_page' => $posts_per_page,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'status' => 'publish',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)
);
The problem I have is, I also need to check whether a custom field called "post_is_global" has been set (the type is BOOL by the way) but I don't know how to implement it into this query. Any help would be greatly appreciated.
Many thanks!
The query should look somewhat like this:
$args = array(
...
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'post_is_global',
'value' => '1',
'compare' => '=',
),
)
);
$query = new WP_Query($args);
References:
ACF | Query posts by custom fields
Class Reference/WP_Query

Categories