Woocommerce filter query backend - php

How do I filter the product to specific things?
I got the next code:
$post_query .= array(
'relation' => 'AND',
array(
'post_type' => 'tc_events',
'post_author' => $user_ID
),
array(
'post_type' => 'tc_templates',
'post_author' => $user_ID
),
array(
'post_type' => 'tc_templates',
'post_title' => 'Default'
),
);
$query->set( $post_query );
I would like to filter out some post_types to specific author and a post_type to a post title.
I guess I got something wrong, I don't know how edit the post query...

Related

Wordpress filter by taxonomy ACF field

I have a problem searching for posts for which I have created a special field in taxonomy.
$args = array(
'posts_per_page' => '20',
'paged' => $paged,
'post_type' => 'cars',
'order' => 'DESC',
);
Taxonomy name: localization
With my cars post type I have a taxonomy relation, where there is a field I created called "city".
How can I filter posts from "cars" post type by this custom field in taxonomy in wp_query?
I tried to write such tax_query, but I keep doing something wrong. Can you give me an example where someone filters it in a similar way by custom field?
Your query would look something like:
$args = array(
'post_type' => 'cars',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'localization',
'field' => 'slug',
'terms' => array( 'tax1', 'tax2' )
)
)
);
$query = new WP_Query( $args );

How to query posts by ACF field but then also order by a separate ACF field?

I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );

Fetch WordPress posts and order by meta_key's ID

I am trying to fetch the post by meta_key in WordPress, and now I want to order posts by the meta_key's ID.
My code:
$args = array(
'post_type' => 'post',
'posts_per_page' => 15,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'on',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
I want to order them based on the time meta_key was created. How can I accomplish that?

wordpress the_post method in custom category page not showing top viewed post

I have Most viewed posts(in Customer Category), in home page I have added this widget and it works perfect, in detail page also it works perfect but in category page the same widget behaves strangely
$r = new WP_Query( array( 'tax_query' =>
array(
'relation' => 'OR',
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)),
),
'category__in'=>array(10),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC' ) );
if ($r->have_posts()) :
// Enters this block in home page and detail page
while ( $r->have_posts() ) : $r->the_post();
else:
// Enters this block in category page
Anyone know why this strange behavior is?
Maybe you have some filter that is affecting categories page, add this to your query args:
'suppress_filters' => true
You can also try adding wp_reset_query() after your main query, but this should not be necessary because you are declaring a new WP_Query.
I just realize that your query args are wrong. You have set a relation, but you only have one taxonomy array. Also you don't need the 'category__in'=>array(10) argument, it is only for the default taxonomy "category" and you want only posts from your custom taxonomy "custcategory", isn't it?
Your query should be:
$r = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'custcategory',
'field' => 'term_id',
'terms' => array(10)
),
),
'post_type'=> $post_type ,
'posts_per_page' => $number,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));

Showing posts using custom taxonomy name - wordpress

I have the following line of code:
$args = array( 'category_name' => the_slug(), 'post_type' => 'Feature', 'posts_per_page' => 2, 'orderby'=> rand );
Which works fine in showing two posts from the category, as long as the category name is the same as the slug of the current page. I need to change category name to my custom taxonomy, this is called 'featuring'. I've tried changing category_name to featuring_name but it just showed all posts instead of only ones from that taxonomy.
Ended up with the following:
$args = array(
'showposts' => -0,
'post_type' => 'feature',
'orderby' => rand,
'tax_query' => array(
array(
'taxonomy' => 'featuring',
'field' => 'slug',
'terms' => the_slug()
)
)
);

Categories