How to exclude entries with custom taxonomy from the main query - wordpress

I need to exclude custom post type entries with a specific custom taxonomy from the main query. I have reviewed other similar entries on SE but can't make it work.
This is what I have:
$args = array(
'post_type' => 'pojazdy', //my cpt name
'tax_query' => array(
array(
'taxonomy' => 'oferta_aktywna', //my custom taxonomy name
'field' => 'slug',
'terms' => 'sprzedane', //specific custom category
'operator' => 'NOT IN',
),
)
);
$query = new WP_Query( $args );

Related

How to show all custom post from custom taxonomy?

I have a custom post called item and custom taxonomy under this custom post called item_category
Now, I want to get all posts based on this custom taxonomy.
To do that, I have this URL of custom taxonomy:
http://localhost/mysite/item_category/event/
Here, item_cateogry is the custom taxonomy and event is the term of this taxonomy.
So, my code is below but not showing anything.
$queried_object = get_queried_object();
$current_category_name = $queried_object->slug; // will print the `event`
$items = new WP_Query(array(
'post_type' => 'item',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $current_category_name,
'field' => 'slug',
),
),
));
I have found the issues:
Here is the right code:
$items = new WP_Query(array(
'post_type' => 'item',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'item_category',
'field' => 'slug',
'terms' => $current_category_name
),
)
));

How to display post with two custom taxonomy in wordpress

I have a custom post type - Poems.
And I create a two taxonomy - Cities and Authors.
When I clicked on "Paris" menu item - I need to displayed all Authors and his list of poems in that city.
How to do it ?
Thanks.
You'll likely want to take a look at the Codex entry for WP Query. That gives you all the information you could want on creating custom WordPress queries. Including taxonomy arguments.
You'd want to pass to tax query arguments using the AND relation. Something along the lines of this example:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => 'london',
),
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => 'dickens',
),
),
);
$query = new WP_Query( $args );
Edit: 10/30/17 per comment below
If you were saving your author's as post meta you could order by that. But if your author is also a taxonomy it gets a little trickier since there isn't a good way to order by a taxonomy. See this related question.
Instead we could get all of our author taxonomy terms using get_terms.
$authors = get_terms( 'author', array(
'hide_empty' => false,
) );
That gives us all author terms that have at least one post assigned to them.
Now that we have the terms we could run through each author and run a query to get the poems for that author in that city. Like so:
foreach ( $authors as $author ) {
$args = array(
'post_type' => 'poem',
'tax_query' => array(
relation => 'AND',
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => 'london',
),
array(
'taxonomy' => 'author',
'field' => 'term_id',
'terms' => $author->term_id,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
// Put your post markup here
endwhile;
endif; wp_reset_query();
}
If you are not good with coding you can use free Content Views plugin, and combine that yyou have to combine. I started like this.

Get post type and category

On my frontpage (index.php) I would like to display all posts of the type 'event' as well as the category 'main' of the type 'post'. How can I merge those two conditions? In my current code, I can filter the two post-types but not the category 'main'.
<?php global $wp_query; $args = array_merge( $wp_query->query, array( 'post_type' => array('post','event') ));
query_posts( $args ); ?>
Just change the $args to this:
<?php
$args = array(
'post_type' => array('post','event'),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => 'main',
'field' => 'slug'
),
array(
'taxonomy' => 'event_tag', // this needs to be whatever custom taxonomy you have declared for your custom post type.
'terms' => 'main',
'field' => 'slug'
),
)
);
?>

Wordpress query not filtering by taxonomy when specifying a slug

I have a custom post type called news. Inside that I have two custom taxonomies called press-releases and media-coverage. When I query for multiple posts with this query:
$args = array(
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 15,
'paged' => $json_api->query->page,
);
$query = new WP_Query($args);
It only gets news posts categorized as press-releases like I want. But if I specify a slug that exists in media-coverage it returns that post even though I have the press-releases taxonomy specified. Example:
$args = array(
'name' => $slug,
'post_type' => 'news',
'news_type' => 'press-releases',
'posts_per_page' => 1,
);
You may try this
$args = array(
'post_type' => 'news',
'name' => '$slug,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_type',
'field' => 'slug',
'terms' => 'press-releases'
)
)
);

Querying custom post type with two criteria

Thanks in advance for any help.
I've created a page that lists the taxonomy of a custom-post-type. The issue I'm running into is the page that links from this listing should only pull the category and a placement code (in the example that's standard). It's pulling the placement but how do I get it to add in the dynamically created category pulled from the previous page.
Code that dynamically pulls taxonomy (category header). This pulls the right category on the page.
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name;
Code below that pulls on the particular placement. How do I add the dynamically pulled taxonomy pulled by code above so content meets both criteria.
$args = array(
'post_type' => 'buyersguide',
'meta_query' => array(
array(
'key' => 'Placement',
'value' => 'standard',
'compare' => 'LIKE'
)
)
);
query_posts($args);
You can fix this by using the following code:
$args = array(
'post_type' => 'buyersguide',
'meta_query' => array(
array(
'key' => 'Placement',
'value' => 'standard',
'compare' => 'LIKE'
)
),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug
)
)
);
query_posts($args);
For more information on taxonomy parameters, go to: Taxonomy Parameters - WP_Query

Resources