Wordpress QP_Query operator - wordpress

I trying to create a query to get some news from category A,B,C
I wont a article which is in all categories.
Array
(
[post_type] => catalog
[order] => DESC
[orderby] => date
[status] => publish
[paged] => 1
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => catalog_category
[field] => id
[include_children] => 1
[operator] => AND
[terms] => Array
(
[0] => 12
[1] => 17
[2] => 43
)
)
)
)
If I use "operator" "IN" I get all articles if I use "AND" I get none results.
List of my cats and articles:
As you can see, I have only 1 article in all 3 categories.
What is my mistake?

Your query seems fine but not sure what is wrong, anyways, as an alternative you may give it a try to get all posts (custom post type = catalog) which belongs to all three categories in (a, b and c), not less/more but exact match of all categories.
$cat_Ids = array();
// get id from each category name
foreach(array('a', 'b', 'c') as $cat) {
$cat_Ids[] = get_cat_ID($cat)
}
$args = array(
'post_type' => 'catalog', // <-- post type is 'catalog', not 'news', right ?
'category__and' => $cat_Ids,
'orderby' => 'date',
'order' => 'DESC',
'status' => 'publish'
);
$query = new WP_Query($args);

Related

Comparing arrays in WP_Query with custom fields

I'm having an issue with comparing values between two arrays in wp_query
I have the two roles administrator and forhandler_salg ($roles returns an array with these two values), but it only queries posts with the "public" key set to true
I got more posts when I changed the "allowed_userroles" key compare value to "LIKE", but those aren't the right posts
$roles = "";
if (is_user_logged_in()) {
$user = wp_get_current_user();
$roles = (array) $user->roles;
}
$args = array(
'post_type' => 'downloads',
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'public',
'value' => true,
'compare' => '=',
),
# Not working
array(
'key' => 'allowed_userroles',
'value' => $roles,
'compare' => 'IN',
),
),
);
print_r($args) returns
Array
(
[post_type] => downloads
[posts_per_page] => -1
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[key] => public
[value] => 1
[compare] => =
)
[1] => Array
(
[key] => allowed_userroles
[value] => Array
(
[16] => administrator
[17] => forhandler_salg
)
[compare] => IN
)
)
)
And a post with the "public" custom field as true and some added roles returns this, when I print_r() the allowed_userroles for the post:
Array
(
[0] => role1
[1] => role2
[2] => role3
)
How would I go about checking if allowed_userroles in the query has values that are also present in $roles?
EDIT: "like" works if I put in a role manually
Ok, for these kinds of problems, it's easier to start with your basic array of arguments and then modify it.
Since this question is tagged with ACF, ACF is likely storing this as a single meta entry. Furthermore, if this is an array of values, ACF is very likely storing it as a serialized PHP string in the database (e.g., a:2:{i:0;s:4:"1234";i:1;s:3:"qed";}).
Let's start with the base code:
$roles = [];
if (is_user_logged_in()) {
$user = wp_get_current_user();
$roles = (array) $user->roles;
}
$args = array(
'post_type' => 'downloads',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'public',
'value' => "1",
'compare' => '=',
),
),
);
In this case, you'll need to use a LIKE to attempt a sub-string match.
foreach($roles as $role) {
$args['meta_query'][] = [
'key' => 'allowed_userroles',
'value' => $role, // can't remember if wp will wrap value with %
'compare' => 'LIKE',
];
}
Ideally, you'd want to match based on the string length as well, but that might be a bit prohibitive. If you keep running into issues getting the exact results out, you could run two separate queries. The first query would be for all public items, and the second query would be for non-public items. You could then take the results of the second query and then filter out values that do not have the correct meta, since it'll be easier to process using get_field('allowed_userroles').

get_posts not return all post, with multiple custom post type

Thank you for read
i have passes $args variable it contain following args.
this args variable pass in get_posts method.
it will give only quiz_4,quiz_0 posts. where i am doing mistake
Array
(
[posts_per_page] => 20
[paged] => 1
[post_type] => Array
(
[0] => quiz_4
[1] => quiz_0
[2] => quiz_5
[3] => quiz_3
[4] => quiz_2
[5] => quiz_1
[6] => password
)
[orderby] => menu_order title
[order] => ASC
[post_status] => any
[suppress_filters] =>
[update_post_meta_cache] =>
)
Try numberposts => 20 instead of posts_per_page.
$args
(array) (Optional) Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.
'numberposts'
(int) Total number of posts to retrieve. Is an alias of $posts_per_page in WP_Query. Accepts -1 for all. Default 5.
Reference:
https://developer.wordpress.org/reference/functions/get_posts/

WooCommerce List Top Selling Products & Categories

I'm trying to write PHP code to list top selling items (with quantity) and top selling categories (with total sales amount) in a tabular form in descending order.
Item | Quantity
Category | Sales
I know we can retrieve this from Reports screen in WooCommerce, but I need to get this values so I can display them else where in the WordPress admin panel in a widget. I know how to create dashboard widgets, by the way.
Does anyone know how to implement this? Any help is appreciated.
Thanks.
you should look into woocommerce files and look for WC_Admin_Report. You can see some examples and work your way with what you want.
I'll give you this example I made.
add_action('init', 'reports');
function reports() {
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
$wc_report = new WC_Admin_Report();
$data = $wc_report->get_order_report_data( array(
'data' => array(
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'quantity'
),
'_line_subtotal' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross'
),
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'order_item_name' => array(
'type' => 'order_item',
'function' => '',
'name' => 'order_item_name',
),
),
'group_by' => 'product_id',
'order_by' => 'quantity DESC',
'query_type' => 'get_results',
'limit' => 20,
'order_status' => array( 'completed', 'processing' ),
) );
print_r($data);
}
sample output of print_r($data);
Array
(
[0] => stdClass Object
(
[quantity] => 4
[gross] => 140
[product_id] => 53
[order_item_name] => Happy Ninja
)
[1] => stdClass Object
(
[quantity] => 3
[gross] => 36
[product_id] => 70
[order_item_name] => Flying Ninja
)
[2] => stdClass Object
(
[quantity] => 1
[gross] => 10
[product_id] => 50
[order_item_name] => Patient Ninja
)
)
you have the data and that would get you up and running for Item | Quantity.

Single Meta key with multiple values not working in searching

I have search query where i am using single meta_key with multiple values but its not working . here is the meta query .
[meta_query] => Array
(
[0] => Array
(
[key] => _vital_lighting_color
[value] => Array
(
[0] => red
[1] => green
[2] => blue
)
[type] => char
[compare] => IN
)
)
but its not fetching record i don't what's happening can you please check it .
Thanks
Using proper php syntax would help :
$query_args = array(
'meta_query' => array(
'key' => '_vital_lighting_color' ,
'value' => array
('red' , 'green' ,'blue' ) ,
'compare' => 'IN'
)
) ;
I removed type, because "char' is the default value
If it does not work, then run a query on the database directly to check you have posts

Wordpress get taxonomy name with slug

How can I get a taxonomy id or name with only the taxonomy slug ?
I guess I'm looking for the equivalent of get_term_by() but for taxonomies.
Edit : I must specify that I'm trying to get the tax ID of a WooCommerce product attribute.
Thanks
WordPress does provide a function to get the taxonomy information from its slug.
$taxonomy_details = get_taxonomy( $slug );
This will return the taxonomy details as an object, which includes the various labels for the taxonomy. For example here's the returned object when called for the standard Category taxonomy, e.g. get_taxonomy( 'category' );
stdClass Object
(
[labels] => stdClass Object
(
[name] => Categories
[singular_name] => Category
[search_items] => Search Categories
[popular_items] =>
[all_items] => All Categories
[parent_item] => Parent Category
[parent_item_colon] => Parent Category:
[edit_item] => Edit Category
[view_item] => View Category
[update_item] => Update Category
[add_new_item] => Add New Category
[new_item_name] => New Category Name
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
[not_found] => No categories found.
[menu_name] => Categories
[name_admin_bar] => category
)
[description] =>
[public] => 1
[hierarchical] => 1
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] => 1
[show_tagcloud] => 1
[show_in_quick_edit] => 1
[show_admin_column] => 1
[meta_box_cb] => post_categories_meta_box
[rewrite] => Array
(
[hierarchical] => 1
[slug] => category
[with_front] => 1
[ep_mask] => 512
)
[query_var] => category_name
[update_count_callback] =>
[_builtin] => 1
[cap] => stdClass Object
(
[manage_terms] => manage_categories
[edit_terms] => manage_categories
[delete_terms] => manage_categories
[assign_terms] => edit_posts
)
[name] => category
[object_type] => Array
(
[0] => post
)
[label] => Categories
)
Source: https://codex.wordpress.org/Function_Reference/get_taxonomy
As the accepted answer does not answer the question, I provide an answer here even though the question is very old.
The third (required) argument to get_term_by() is the name of the taxonomy itself, and so this function can not be used.
get_taxonomies() can't be used either because then you would have to match the entire rewrite array, which you probably don't have access to.
So the only way I found was to use the private array $wp_taxonomies:
function get_tax_name_from_slug($slug){
foreach ($wp_taxonomies as $key => $value) {
if ($value->rewrite['slug'] === $slug){
return $key;
}
}
}
I really hope Wordpress will provide a way to do this without accessing their internal data structures.
$args = array(
'post_type' => 'awards',
'post_status' => 'publish',
'posts_per_page' => 4,
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'awards_categories',
'field' => 'slug',
'terms' => $award_solution
),
array(
'taxonomy' => 'year',
'field' => 'slug',
'terms' => $yearvalue
),
)
);
how we fetch this with wp select query
<?php
$term = get_term_by('slug', $slug, 'category');
$name = $term->name;
$id = $term->term_id;
?>

Resources