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()
)
)
);
Related
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 );
I want to use Advanced Custom Fields to determine from which category products will be shown. The ACF field gives the ID of the category and from that I can get the slug. I can't get it to work in the array though, and so it shows all products on the page. Does anyone have suggestions or ideas why it's not working? Thanks in advance!
$term_id = get_field('kies_product_categorie'); //Get category id
$term = get_term_by('id', $term_id, 'product_cat'); //Get terms from given category
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug //Slug of given category
)
)
);
Looking at the documentation here, you appear to be missing the field attribute within the tax_query array.
So your $args should look like:
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug, //Slug of given category
'field' => 'slug'
)
)
);
I am creating shortcode with attributes and all attributes working except event_category.
When i add "event_category" attribute it is not giving result as per added category.
Here are my shortcode attributes
// Shortcode Default Array
$default_shortcode_attrs = array(
'type' => 'upcoming',
'search' => 'true',
'event_category' => '',
'events_limit' => '-1',
);
extract(shortcode_atts($default_shortcode_attrs, $attr));
Following are query parameters
$args = array(
'posts_per_page' => -1,
'post_type' => 'event_listing',
'post_status' => 'publish',
'event_listing_category'=> $event_category,
);
$query = new WP_Query($args);
"event_listing_category" is name of custom taxonomy. Please guide me why this query is not fetching the events according to their category.
Any help will be appreciated.
Thanks
Use tax_query instead as below:
I'm assuming you have only one category slug provided in $event_category. If you have more than one category slug in that variable then try converting it to array and replace the whole array($event_category) with the array.
$args = array(
'posts_per_page' => -1,
'post_type' => 'event_listing',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'event_listing_category',
'field' => 'slug',
'terms' => array( $event_category)
)
)
);
$query = new WP_Query($args);
See http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters on how to query for custom taxonomies within your query.
The WP_Query function doesn't understand 'event_listing_category' as an argument, you'll have to tell wordpress that it's a custom taxonomy you want.
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'
));
I have tried everything I can to filter my posts by a custom field taxonomy.
What I have is a custom field with the name of "category" with the field type of "post object", and I am listing all of my pages on my site to choose from.
When I am on my blog post I select an item from the dropdown menu, and now what I want to do is to get the specific post that has the "category" post_title of the current page I am on.
For instance:
I am on the page "Construction", and in my blog post I have selected this page from the list of pages. Now I want to find the latest blog post with the "Category" post_title of "Construction".
My code:
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'post_title',
'terms' => $pageName
)
),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );