WordPress custom query by meta_key - wordpress

I'm trying to figure out how to achieve a specific query to modify my search results for posts with WordPress. I'm trying to search via a custom field called "Common Authors".
There can be multiple Common authors, which is causing my query to sometimes fail. Here is what I've got for now:
<?php
...
$query->set('meta_key', 'common_authors');
$query->set('meta_value', serialize( array(strval($_GET['common_author'])))); // I get a single ID from the url as a string
$query->set('meta_compare', 'IN');
This is what I see when I var_dump the query:
'meta_key' => string 'common_authors'
'meta_value' => string 'a:1:{i:0;s:5:"17145";}'
This works fine if there is only one common_author.
However, there can be multiple common_authors for a post. Here is a meta_value example from the database:
a:4:{i:0;s:5:"14409";i:1;s:5:"17145";i:2;s:5:"14407";i:3;s:5:"14406";}
Could somebody help me out, to figure out how to adapt my query, so that it would return this one as well?

Try This One Work Prefect!
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
if You Need To Use It In pre_get_posts
$meta_query = array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
);
$query->set('meta_query', $meta_query);

Related

Wp Meta query does not work while meta value does

I have two queries which I believed should both work because I believe they are exactly the same
The one is as follows ( Works perfectly, and gives me the posts I need )
$args2 = array(
'post_type' => 'vacancy',
'meta_key' => 'full_or_part_time',
'meta_value' => 'part_time',
)
The other one is as follow : ( Returns all posts, ignores the meta query )
$args2 = array(
'post_type' => 'vacancy',
'meta_query' => array(
array(
'key' => 'full_or_part_time',
'value' => "part_time",
)
),
);
I cannot figure out why option two does not work. Any assistance will be greatly appreciated.

Query only custom fields with wordpress

I am executing the following WP_Query on my mysql database:
$products = new WP_Query(array(
'post_type' => 'posts',
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => '=',
'value' => '_cegg_data_Amazon',
),
),
));
When I var_dump($product) I am receiving a long wp_Query object. However, I would only like to receive an object/list of the custom post fields. The below SQL query gives me exactly that back within phpmyadmin:
SELECT * FROM `wp_postmeta` where meta_key="_cegg_data_Amazon" ORDER BY `wp_postmeta`.`meta_key` ASC
Any suggestions how to get only the values of the custom fields _cegg_data_Amazon. I appreciate your replies!
I think the easiest way would be to loop through your WP_Query object and create your array (or new obj) with the desired field:
$products = new WP_Query(array(
'post_type' => 'posts',
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => '=',
'value' => '_cegg_data_Amazon',
),
),
));
$cegg_data = array(); // initiate the new array
if( $products->have_posts() ) :
while( $products->have_posts() ) : $products->the_post();
$cegg_data[] = get_post_meta(get_the_ID(), '_cegg_data_Amazon', true); // populate array with the new value
endwhile;
endif;
wp_reset_query();
Now, if you var_dump "$cegg_data" it should contain an indexed array with the custom field values.
NB: the code is untested, just a quick draft ;)
Another and solution you can explore would be "posts_fields" filter documented here: https://developer.wordpress.org/reference/hooks/posts_fields/
Hope this helps and good luck ;)
Francesco

Multiple custom field sorting on wordpress archive page

I have been through several topics on sorting taxonomies and custom fields, including some that were promising. However, I am still not constructing my query args correctly it seems.
I am "fixing" someone else's code in a custom theme that is used in a multisite configuration, but I am able to override/filter the query on a per-site basis.
The query is called via the theme with the following:
return apply_filters( 'theme_query_args_filter', $args);
I have custom fields in the "role" taxonomy that include:
last_name
first_name
Pretty common I think.
However, when the args are executed, the following filter args are sorting only by last name (key part is the else clause):
function my_args_filter( $args ) {
if (is_home() && !is_search()) {
$args['meta_key'] = 'event_date';
$args['orderby'] = 'event_date';
$args['order'] = 'DESC';
}
else {
$tax = $args['taxonomy'];
$theterm = $args['term'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
}
add_filter( 'theme_query_args_filter', 'my_args_filter' );
I've tried to modify the orderby as indicated in https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ to use an array to do a multisort, but I'm hitting my head up against the wall. I think the problem is that the code is written using a mixture of old ways of doing things and new ways.
Any advice is appreciated. According to the example on in the docs above, I SHOULD be able to pass in multiple meta key/value/orders via an array, but I'm just not getting it.
Thanks for any leads you might have. (long-time listener, first-time caller)
(I also looked at https://wordpress.stackexchange.com/questions/109849/order-by-desc-asc-in-custom-wp-query but I couldn't extrapolate that example to this one either)
Figured it out on my most recent try. Hat tip to this solution https://wordpress.stackexchange.com/questions/249881/multiple-custom-fields-for-orderby-in-wp-query by Nath. if there is a more elegant way to do it, please let me know.
$tax = $args['taxonomy'];
$theterm = $args['term'];
$paged = $args['paged'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'paged' => $paged,
'posts_per_page' => '10',
'meta_query' => array(
array(
'relation' => 'AND',
'last_name' =>
array(
'key' => 'last_name',
'compare' => 'EXISTS',
),
'first_name' =>
array(
'key' => 'first_name',
'compare' => 'EXISTS',
),
),
),
'orderby' => array (
'last_name' => 'ASC',
'first_name' => 'ASC',
)
);
}

Wordpress Query on custom fields

My site has woocommerce plugin, so post_type is product. Each product has several custom fields used by another plugin (Compare Products Pro).
Now we would like to query products by URL so eg: www.domain.com/?post_type=product&custom_field=value
Is this possible? And how?
Any help would be highly appriciated!
You could add something like this to your query arguments:
'post_type' => $_GET['post_type'],
'meta_query' => array(
array(
'key' => '_woo_compare_field-',
'value' => $_GET['_woo_compare_field-'],
'compare' => '=',
)
),
But its very static, will not work for different meta key value pairs.
You could assign them to an array and append that array like this
$meta_queries = array(
'relation' => 'AND',
);
foreach($_GET as $key => $value) {
$result = array(
'key' => $key,
'value' => $value,
'compare' => '=',
);
$meta_queries[] = $result;
}
And append the $meta_queries array to the wordpress query arguments.
But this means all your get variables are used as meta key value pairs. You can ofcourse write some logic for this but it will never be pretty

Wordpress WP_Query using filter, custom post types, and custom fields will not return correct post types

I am trying to get results for certain post_types, however, when ever I add the filter to check if the post_title and post_content are "LIKE" the query string, all post_types are returned, including attachment post_types, which I do not want as show in the $args. Here is the code I am trying:
$query_string = get_search_query();
$args = array(
'post_type' => array("hc_pillow", "hc_topper", "hc_accessory", "page"),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tab_retail',
'value' => $query_string,
'compare' => 'LIKE'
),
array(
'key' => 'tab_tech',
'value' => $query_string,
'compare' => 'LIKE'
),
array(
'key' => 'tab_shipping_information',
'value' => $query_string,
'compare' => 'LIKE'
)
),
'posts_per_page' => "-1"
);
function filter_where_title($where = '') {
$query_string = get_search_query();
$where .= " OR post_title LIKE '%".$query_string."%' OR post_content LIKE '%".$query_string."%'";
return $where;
}
$posts = new WP_Query();
add_filter('posts_where', 'filter_where_title');
$posts->query($args);
remove_filter('posts_where', 'filter_where_title');
Any help at all would be appreciated, I'm not sure what else to try.,
Your WHERE statement should be structured like that:
WHERE `post_type` IN ( 'hc_pillow', 'hc_topper', 'hc_accessory', 'page' ) AND ( `post_title` LIKE '%search_string%' OR `post_content` LIKE '%search_string%' )
Now it's more like
WHERE `post_type` IN ( 'hc_pillow', 'hc_topper', 'hc_accessory', 'page' ) OR `post_title` LIKE '%search_string%' OR `post_content` LIKE '%search_string%'
where any single condition of the three would be enough to envalidate a row.

Resources