I'm trying to make a call using a custom end-point to one custom post type on my project.
I'm passing dynamic parameters to that function-
// $per_page = $_GET['per_page'];
// $page = $_GET['page'];
$per_page = 18;
$page = 1;
$type = $_GET['type'];
$order = $_GET['order'];
// $houseType = $_GET['houseType'];
$houseType = 'Terreno';
$houseState = $_GET['houseState'];
$houseArea = $_GET['houseArea'];
$housePrice = $_GET['housePrice'];
$houseRooms = $_GET['houseRooms'];
// $houseProp = $_GET['houseProp'];
$houseProp = 'venda';
$meta_query = array();
if( $houseProp ) {
$meta_query[] = array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
);
}
if ( $houseType ) {
$meta_query[] = array(
'key' => 'tipo_de_imovel',
'value' => $houseType,
'compare' => '=',
);
}
if ( $houseState ) {
$meta_query[] = array(
'key' => 'estado_do_imovel',
'value' => $houseState,
'compare' => '=',
);
}
if ( $houseArea ) {
$meta_query[] = array(
'key' => 'area_bruta',
'value' => $houseArea[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'area_bruta',
'value' => $houseArea[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $housePrice ) {
$meta_query[] = array(
'key' => 'preco',
'value' => $housePrice[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'preco',
'value' => $housePrice[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $houseRooms ) {
$meta_query[] = array(
'key' => 'quartos',
'value' => $houseRooms[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'quartos',
'value' => $houseRooms[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $type == "preco") {
$orderBy = array(
'meta_key' => $type,
'orderby' => 'meta_value_num',
'order' => $order,
);
} else {
$orderBy = array(
'orderby' => array(
'date' => $order,
'menu_order'=> $order,
)
);
}
$args = array_merge($orderBy, array(
'post_type' => 'imoveis',
'posts_per_page' => $per_page,
'paged' => $page,
'meta_query' => $meta_query,
));
// get posts
$posts = get_posts($args);
The problem is that the $meta_query is not working...
If I pass only 1 parameter (houseProp), I can make that work with $meta_query[0], but if I pass more parameters it does not return anything.
What am I doing wrong?
EDIT:
I was able to see that I need to update my post on the backend (by pressing the update button).
I'm creating a post like this:
$post_id = wp_insert_post(array(
'post_title' => $house->InfoGeral->SubTipoImovel,
'post_type' => 'imoveis',
'post_content' => $description[0],
'post_status' => 'publish')
);
and filling the ACF custom fields like this:
update_field('field_5d230e2057fa5', $ref, $post_id);
what could possibly be the error?
When you use $meta_query it uses the 'relation' attribute and is defaulted to 'AND'. For example, when you set it up with one parameter, like your example:
$meta_query[] = array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
);
You are really setting it to look like:
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
)
);
So now when you add a bunch of other parameters, it really looks like this:
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
),
array(
'key' => 'area_bruta',
'value' => $houseArea[0],
'type' => 'NUMERIC',
'compare' => '>'
),
array(
'key' => 'area_bruta',
'value' => $houseArea[1],
'type' => 'NUMERIC',
'compare' => '<'
)
);
So my guess is you are starting to really narrow your results down to this AND that AND this other thing, which is probably too restrictive. You can change the relation attribute to 'OR', if that is your goal, or attempt to re-code it to be less restrictive.
You can fix this by changing your line of:
$meta_query = array();
to:
$meta_query = array( 'relation' => 'OR' );
and keep everything else the same.
Related
UPDATE: I mean to search user in the Admin User Dashboard at /wp-admin/users.php
Is there a way to search users using 'first_name' and 'last_name'?
I found this plugin but not working at all: https://wordpress.org/plugins/full-name-search-in-wp-admin/
I also found this function, but neither is working in my site. Do I need to call the function with a hook/action?:
function search_users($search_term){
global $wpdb;
$the_users = $wpdb->get_results("SELECT DISTINCT $wpdb->users.* FROM $wpdb->users INNER JOIN $wpdb->usermeta um1 ON um1.user_id = $wpdb->users.ID JOIN $wpdb->usermeta um2 ON um2.user_id = $wpdb->users.ID WHERE (um1.meta_key = 'uRPhICRS_capabilities' AND um1.meta_value LIKE '%agent%') AND ((um2.meta_key = 'first_name' OR um2.meta_key = 'last_name') AND um2.meta_value LIKE '%".$search_term."%');");
if(!empty($the_users)){
return $the_users;
}
else{
return false;
}
}
Two Ways as follow:
1) If You want to check from the string match in either firstname or lastname, following will be work:
$users_query = new WP_User_Query(
array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $str,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $str,
'compare' => 'LIKE'
)
)
)
);
$users = $users_query->get_results();
2) If you want to check the user from username, user_nicename, user_email, firstname, or lastname use following code:
//search from the user table
$users_query_table = new WP_User_Query(
array(
'search' => "*{$str}*",
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
),
) );
$users_via_table = $users_query_table->get_results();
//search from the usermeta
$users_query_meta = new WP_User_Query(
array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $str,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $str,
'compare' => 'LIKE'
)
)
)
);
$users_via_meta = $users_query_meta->get_results();
// Merge both result..
$combined_users = array_merge( $users_via_table, $users_via_meta );
// Get unique user
$users = array_unique( $combined_users, SORT_REGULAR );
You can use WP_User_Query.
Example:
// The search term
$search_term = 'Ross';
// WP_User_Query arguments
$args = array (
'role' => 'reporter',
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'description',
'value' => $search_term ,
'compare' => 'LIKE'
)
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
https://developer.wordpress.org/reference/classes/wp_user_query/
I am having a issue with wordpress sorting. For some reason its not putting the lowest number on top and highest on bottom. For example of results.
500
500
1
1000
1000
2000
I thought the problem was because of sort_priority but its all set to 0 for priority
Edited. Below is the actual PHP code that I used to make the query
<?php
if($post_type == 'cds')
{
$meta_query = array();
$meta_query[] = array(
'key' => 'lender_type',
'value' => 'CDs',
);
// <=
if(isset($_POST['minimum_deposit']) && !empty($_POST['minimum_deposit']))
{
$minimum_deposit = trim($_POST['minimum_deposit']);
$minimum_deposit = (int) $minimum_deposit;
$meta_query[] = array(
'key' => 'minimum_deposit',
'value' => $minimum_deposit,
'type' => 'numeric',
'compare' => '<='
);
}
// desposit_term
if(isset($_POST['deposit_term']) && !empty($_POST['deposit_term']))
{
$deposit_term = trim($_POST['deposit_term']);
$meta_query[] = array(
'key' => 'deposit_term',
'value' => $deposit_term,
'compare' => 'LIKE'
);
}
// rating
if(isset($_POST['rating']) && !empty($_POST['rating']))
{
$ratings = trim($_POST['rating']);
$list = explode('-', $ratings);
if(sizeof($list) == 1)
{
$meta_query[] = array(
'key' => 'rating',
'value' => $list[0],
'type' => 'numeric',
'compare' => '<='
);
}
if(sizeof($list) > 1)
{
$meta_query[] = array(
'key' => 'rating',
'value' => $list[0],
'type' => 'numeric',
'compare' => '>='
);
}
}
// sort_by
if(isset($_POST['sort_by']) && !empty($_POST['sort_by']))
{
$sort_by = trim(strtolower($_POST['sort_by']));
}
if(!isset($sort_by))
{
$sort_by = 'apy_rate';
}
$ordering = 'DESC';
if($sort_by == 'minimum_balance_for_apy')
{
$ordering = 'ASC';
$meta_query[] = array(
'minimum_balance_for_apy' => array(
'key' => 'minimum_balance_for_apy',
'compare' => 'EXIST'
)
);
}
if($sort_by == 'apy_rate')
{
$meta_query[] = array(
'apy_rate' => array(
'key' => 'apy_rate',
'compare' => 'EXIST'
)
);
}
if($sort_by == 'minimum_deposit')
{
$meta_query[] = array(
'minimum_deposit' => array(
'key' => 'minimum_deposit',
'compare' => 'EXIST'
)
);
}
// The Query
$args = array(
'post_type' => 'lenders',
'posts_per_page' => -1,
'meta_query' => array('relation' => 'AND', $meta_query),
'orderby' => array(
'sort_priority' => 'DESC',
$sort_by => $ordering
)
);
$the_query= new WP_Query($args);
?>
I fixed by adding 'type' => 'numeric'
Example:
$meta_query[] = array(
'minimum_balance_for_apy' => array(
'key' => 'minimum_balance_for_apy',
'type' => 'numeric',
'compare' => 'EXIST'
)
);
Thanks for help guys.
I need help with a WP_Query. I use the meta query argument using the OR and AND relations, but this argument seems to be ignored in the result of the query.
Here is my code :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'author' => $contact_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $user_id )
),
array(
'relation' => 'AND',
array( 'author' => $user_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $contact_id )
)
)
);
$query = new \WP_Query( $args );
$response = $query->posts;
I already tried to add this argument like suggested in here:
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
Even if I remplace the value of $user_id and $contact_id directly in the query with any number, the query still return the same result. I don't understand why it's not working.
Thank you for your help !
As suggested by dafoxuk, I have to remplace meta_key by key and meta_value by value. I also had to add 'compare' => 'LIKE'.
But event in this case it wasn't working. I also had to stock the author_id in a post_meta, and change the array( 'author'=> $contact_id ) condition by :
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
So the final $args array look like this :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $user_id, 'compare' => 'LIKE '
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $contact_id, 'compare' => 'LIKE' )
)
);
I created an ajax filter on custom post type filter is working fine when I select 10-12 filters but after that admin-ajax.php file gone to pending, I don't know what is the issue.
Website url : http://mustafafazal.com/intranet/carrier-filter-tool/
you can check this issue in dev mode > network in chrome.
https://i.stack.imgur.com/J0Rso.png
https://i.stack.imgur.com/uzug8.png
function misha_filter_function(){
$nested_condition = array();
if (isset($_POST['state']) && !empty($_POST['state'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'state',
'value' => $_POST['state'],
//'compare' => 'LIKE'
),
);
}
if (isset($_POST['age']) && !empty($_POST['age'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'age_min',
'value' => $_POST['age'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'age_max',
'value' => $_POST['age'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['months']) && !empty($_POST['months'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'months_of_exp_min',
'value' => $_POST['months'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'months_of_exp_max',
'value' => $_POST['months'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['driver_type']) && !empty($_POST['driver_type'])){
// Get the selected options
$valueFront1 = $_POST['driver_type'];
// echo print_r($valueFront); die();
// Build the meta query based on the selected options
$meta_query = array('relation' => 'AND');
foreach( $valueFront1 as $item1 ){
$nested_condition[] = array(
'key' => 'driver_type',
'value' => $item1,
//'compare' => 'LIKE',
);
}
}
if (isset($_POST['division_requested']) && !empty($_POST['division_requested'])){
// Get the selected options
$valueFront2 = $_POST['division_requested'];
// echo print_r($valueFront); die();
// Build the meta query based on the selected options
$meta_query = array('relation' => 'AND');
foreach( $valueFront2 as $item2 ){
$nested_condition[] = array(
'key' => 'division',
'value' => $item2,
//'compare' => 'LIKE',
);
}
}
if (isset($_POST['freight_type']) && !empty($_POST['freight_type'])){
// Get the selected options
$valueFront3 = $_POST['freight_type'];
// echo print_r($valueFront); die();
// Build the meta query based on the selected options
$meta_query = array('relation' => 'AND');
foreach( $valueFront3 as $item3 ){
$nested_condition[] = array(
'key' => 'freight_type',
'value' => $item3,
//'compare' => 'LIKE',
);
}
}
if (isset($_POST['home_time_requested']) && !empty($_POST['home_time_requested'])){
// Get the selected options
$valueFront4 = $_POST['home_time_requested'];
// echo print_r($valueFront); die();
// Build the meta query based on the selected options
$meta_query = array('relation' => 'AND');
foreach( $valueFront4 as $item4 ){
$nested_condition[] = array(
'key' => 'home_time',
'value' => $item4,
//'compare' => 'LIKE',
);
}
}
if (isset($_POST['additional_options']) && !empty($_POST['additional_options'])){
// Get the selected options
$valueFront5 = $_POST['additional_options'];
// echo print_r($valueFront); die();
// Build the meta query based on the selected options
$meta_query = array('relation' => 'AND');
foreach( $valueFront5 as $item5 ){
$nested_condition[] = array(
'key' => 'additional_options',
'value' => $item5,
//'compare' => 'LIKE',
);
}
}
if (isset($_POST['sign_on_bonus_requested']) && !empty($_POST['sign_on_bonus_requested'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'sign_on_bonus',
'value' => $_POST['sign_on_bonus_requested']
//'compare' => 'LIKE'
)
);
}
if (isset($_POST['careless_years']) && !empty($_POST['careless_years'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'careless_min',
'value' => $_POST['careless_years'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'careless_max',
'value' => $_POST['careless_years'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['reckless_years']) && !empty($_POST['reckless_years'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'recklace_min',
'value' => $_POST['reckless_years'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'recklace_max',
'value' => $_POST['reckless_years'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['dui']) && !empty($_POST['dui'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'dui_in_cmv',
'value' => $_POST['dui'],
//'compare' => 'LIKE'
)
);
}
if (isset($_POST['dui_years']) && !empty($_POST['dui_years'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'dui_yrs_min',
'value' => $_POST['dui_years'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'dui_yrs_max',
'value' => $_POST['dui_years'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['moving_violations']) && !empty($_POST['moving_violations'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'no_of_moving_violations',
'value' => $_POST['moving_violations'],
'type' => 'NUMERIC',
'compare' => '='
)
);
}
if (isset($_POST['dot_recordable']) && !empty($_POST['dot_recordable'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'dot_recordable_min',
'value' => $_POST['dot_recordable'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'dot_recordable_max',
'value' => $_POST['dot_recordable'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['preventable']) && !empty($_POST['preventable'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'preventable_min',
'value' => $_POST['preventable'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'preventable_max',
'value' => $_POST['preventable'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['job_years']) && !empty($_POST['job_years'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => '#_of_jobs_min',
'value' => $_POST['job_years'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => '#_of_jobs_max',
'value' => $_POST['job_years'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
if (isset($_POST['terminated']) && !empty($_POST['terminated'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'terminated',
'value' => $_POST['terminated'],
//'compare' => 'LIKE'
)
);
}
if (isset($_POST['safety_reasons']) && !empty($_POST['safety_reasons'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'safety_term',
'value' => $_POST['safety_reasons'],
//'compare' => 'LIKE'
)
);
}
if (isset($_POST['last_job']) && !empty($_POST['last_job'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'last_job_term',
'value' => $_POST['last_job'],
//'compare' => 'LIKE'
)
);
}
if (isset($_POST['alcohol_test']) && !empty($_POST['alcohol_test'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'failed_da_test',
'value' => $_POST['alcohol_test'],
//'compare' => 'LIKE'
)
);
}
if (isset($_POST['how_long']) && !empty($_POST['how_long'])){
$nested_condition[] = array(
'relation' => 'AND',
array(
'key' => 'failed_when_min',
'value' => $_POST['how_long'],
'type' => 'NUMERIC',
'compare' => '<='
),
array(
'key' => 'failed_when_max',
'value' => $_POST['how_long'],
'type' => 'NUMERIC',
'compare' => '>='
)
);
}
$args = array(
'post_type' => 'carrier',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => $nested_condition
);
// query
$the_query = new WP_Query( $args );
//echo $the_query->request; die();
?>
<?php
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="carrier-box">
<h2><?php the_title(); ?></h2>
<a class="read-more" href="<?php the_permalink(); ?>">Website</a>
</div>
<?php
endwhile;
endif;
?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php
die();
}
add_action('wp_ajax_myfilter', 'misha_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
I'm having issues with my WP_Query. So I have two select boxes created using ACF one for Country and one for sector. When I select country and leave sector empty I get all the posts with the selected country and like wise for the sector. What I need to do is filter it further so if both are selected and not just one I get all posts from that country and sector. My args below any help is appreciated.
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dis_country',
'value' => $country_sector,
),
array(
'key' => 'dis_sector',
'value' => $sector,
)
)
);
May be it's helpful for you -
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1
);
if($country_sector){
$args['meta_query'][] = array(
'key' => 'dis_country',
'value' => $country_sector
);
}
if($sector){
$args['meta_query'][] = array(
'key' => 'dis_sector',
'value' => $sector
);
}
Please try following code with relation AND & compare operator.
Also please check WP_Query class at https://codex.wordpress.org/Class_Reference/WP_Query
<?php
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : '';
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : '';
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>