How to search for users in WP with serialized meta values - wordpress

I can get users using $all_users = get_users(['meta_key' => 'first_name', 'meta_value' => 'john']); but I have a small problem now, as the meta value for feature I am trying to fetch is serialized.
I have copied a row from the table as shown below.
103 wp_capabilities a:1:{s:12:"cs_candidate";b:1;}
I can get the value and unserialize it, but not sure if there is a way to do this in WP.
UPDATE:
To add more clarification, I am referring to this page https://codex.wordpress.org/Function_Reference/get_users or the below function to be exact:
<?php $args = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => '',
'role__in' => array(),
'role__not_in' => array(),
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
'date_query' => array(),
'include' => array(),
'exclude' => array(),
'orderby' => 'login',
'order' => 'ASC',
'offset' => '',
'search' => '',
'number' => '',
'count_total' => false,
'fields' => 'all',
'who' => ''
);
get_users( $args ); ?>
The 'meta_value' => '', part in the field is serialized a:1:{s:12:"cs_candidate";b:1;}

You can't search unserialize an search at the same time, I'm afraid. Searching in serialized data is always a bit of a hassle.
If you are certain of structure of the data you are searching, you could do something like:
$meta_args = [
'key' => 'wp_capabilities',
'value' => 'cs_candidate";b:1',
'compare' => 'LIKE'
];
$users = get_users(['meta_query' => $meta_args]);
It's anything but pretty, but should do the trick.

Related

Unable to sort Orders on customer order page by meta value

I am trying to filter WC orders on customer order dashboard page using woocommerce_my_account_my_orders_query filter. and Order by date, title etc are working fine for me but when i try to filter orders by custom meta keys and values then it’s not returning me correct result.
add_filter( 'woocommerce_my_account_my_orders_query', 'af_filter_orders', 10, 1 );
function af_filter_orders( $orders ) {
$orders = array(
'limit' => -1,
'offset' => null,
'page' => 1,
'meta_key' => 'custom_meta_key', //meta type is plain string and i need results alphabetically.
'orderby' => 'meta_value', //meta_value_num
'order' => 'DESC', //ASC
'customer' => get_current_user_id(),
'paginate' => true
);
return $orders;
}
I am on WC v2.6. have not tested on 3x yet. and i need it working with 2.6
add_filter('woocommerce_my_account_my_orders_query', 'af_filter_orders', 10, 1);
function af_filter_orders($orders) {
$orders = array(
'limit' => -1,
'offset' => null,
'page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'custom_meta_key',
'value' => $value,
'compare' => 'LIKE'
)
), //meta type is plain string and i need results alphabetically.
'orderby' => 'meta_value', //meta_value_num
'order' => 'DESC', //ASC
'customer' => get_current_user_id(),
'paginate' => true
);
return $orders;
}
in WooCommerce below 3.0, woocommerce_my_account_my_orders_query is an array of args used for get_post.
In this, I think your $orders array should include meta_query for it to work correctly.
$orders = array(
'limit' => -1,
'offset' => null,
'page' => 1,
'meta_key' => 'custom_meta_key', //meta type is plain string and i need results alphabetically.
'orderby' => 'meta_value', //meta_value_num
'order' => 'DESC', //ASC
'customer' => get_current_user_id(),
'paginate' => true,
'meta_query' => array(
array(
'key' => 'custom_meta_key', //meta type is plain string and i need results alphabetically.
'value' => array( 3, 4 ),
'compare' => 'IN',
),
),
);
in WooCommerce 3.0.x, woocommerce_my_account_my_orders_query is used for wc_get_orders where your args are limited to what is given. meta_key and meta_query is not acceptable args. And so will not work. However, the query is handle at the end with a filter woocommerce_order_data_store_cpt_get_orders_query that you can use. It is basically calling WP_Query. Where woocommerce_order_data_store_cpt_get_orders_query is the filter for the args.
P.S.
I think in 3.1.x, they've removed the wp_parse_args of wc_get_orders. This means meta_key and meta_query will work when you pass it as parameter.

Is it possible to sort terms by get_term_meta in wordpress

Lets see an example of my args:
$args = array(
'orderby' . => $orderby,
'number' => $per_page,
'offset' => $offset,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'fields' => 'all',
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'offset' => $offset,
'terms' => $term->slug,
'cache_domain' => 'core'
);
If I have a term_meta like this one which is saved in database
$yellow = get_term_meta($term->term_id,'product_yellow',true);
Is it possible to sort only taxonomies with yellow term_meta ?
Yes, it's possible.
$args = array(
'taxonomy' => 'post_tag',
'meta_key' => 'product_yellow',
'orderby' => 'meta_value', // use 'meta_value_num' if the value type of this meta is numeric.
'order' => 'DESC',
);
$terms = get_terms($args);
Do you mean something like this?
$args = array(
'meta_query' => array(
array(
'key' => $term->term_id,
'value' => 'product_yellow',
'compare' => 'LIKE'
)
)
);
That will limit the scope of your query to a specific meta value.

WordPress: First Comment ID from current post

Does someone know how to get the first comment ID from the current post?
I have already searched long for a good solution, but found nothing suitable.
I appreciate any help.
get_comments() returns an object. So you can just access the first one like any other object:
$comments = get_comments();
$first_comment_id = $comments[0]->comment_ID;
You have to use the following function to get comments of a post
<?php get_comments( $defaults ); ?>
It's default usage is as follows:
<?php
$defaults = array(
'author_email' => '',
'author__in' => '',
'author__not_in' => '',
'include_unapproved' => '',
'fields' => '',
'ID' => '',
'comment__in' => '',
'comment__not_in' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_author__in' => '',
'post_author__not_in' => '',
'post_ID' => '', // ignored (use post_id instead)
'post_id' => 0,
'post__in' => '',
'post__not_in' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => 'all',
'type' => '',
'type__in' => '',
'type__not_in' => '',
'user_id' => '',
'search' => '',
'count' => false,
'meta_key' => '',
'meta_value' => '',
'meta_query' => '',
'date_query' => null, // See WP_Date_Query
);
?>
Make sure to edit the values as per your requirement. Using get_comments() you can get all comments and this way you can fetch first or any required comment id using loop.

Create a filter in a page template

I am using Realto theme for wordpress I am wondering if I am on the correct path. I need to display properties from a city. So I decide to create a page template to add the location
This are the default arguments but I dont know how to filter the results by city
$args = array(
'numberposts' => '',
'posts_per_page' => $posts_per_page,
'offset' => 0,
'cat' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'property',
'post_mime_type' => '',
'post_parent' => '',
'paged' => $paged,
'post_status' => 'publish'
);
I tried adding
'locations' => 'MYCITY'
but it didnt work
This is an example of the search results when I search by city, so I am basing my arguments on this.
/?post_type=property&search_keyword=&locations=MYCITY&property_type=proyectos&beds=&baths=&status=&min-price=&max-price=
I assume that locations is a custom field? Maybe this works:
<?php
$args = array(
'post_type' => 'property',
'meta_key' => 'locations',
'meta_value' => 'MYCITY'
);
?>
Visit http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for more details.

How to sort multiple wordpress custom field values?

Display posts with 'Product' type ordered by 'Price' custom field:
$query = new WP_Query(
array ( 'post_type' => 'product',
'orderby' => 'meta_value',
'meta_key' => 'price' )
);
Which code should I use if also want to order by 'Size'?
Another example on which I need multiple sort on custom fields:
Display posts with 'Event' type ordered by 'Start_Hour' and then by 'Start_Minute'.
Thanks to Bainternet I found the solution:
function orderbyreplace($orderby) {
return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
and...
$args = array(
'post_type'=>'Events',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'Start_Hour',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'Start_Minute',
'value' => '',
'compare' => 'LIKE'
)
)
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
I think this changed slightly in Wordpress 3.7.
I had to change
return str_replace('menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
into
return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
Thanks for the initial solution! [Edited]
Here's an example of using more than one meta_key and orderby that I believe should work:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'price',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'size',
'value' => '',
'compare' => 'LIKE'
)
),
'orderby' => 'price size',
'order' => 'ASC'
);
$query = new WP_Query;
$resulting_obj = $query->query($params);
You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.
You don't need any filter or hooks to sort multiple custom fields, if your one of custom field is meta_key and other one is normal column of table, than use these parameters/arguments in your query.
'meta_key' => 'KEY_NAME',
'orderby' => 'meta_value_num SECOND_COLUMN_NAME',
'order' => 'ASC' or 'order' => 'DESC'
Here the order of meta_value_num and SECOND_COLUMN_NAME matter, you may see different-2 result based on the order.

Resources