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 );
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 );
Helllo,
I want to show posts and custom post types , can I change post_type in query as array('post','custom_post_type')
function get_posts( $args = null ) {
$defaults = array(
'numberposts' => 5,
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'suppress_filters' => true,
);
thanks in advance
You can add the post_type as a string (single type) or an array (multiple types).
Example:
$args = array(
'post_type' => array( 'post', 'page', 'movie', 'book' )
);
movie and book above are Custom Post Types.
If you want all post types just:
$args = array(
'post_type' => 'any'
);
The above retrieves any type except revisions and types with exclude_from_search set to true.
More info here
<?php
$args = array(
'post_type' => 'my_post_type',
'post_status' => 'publish',
'posts_per_page' => -1
);
?>
If you want to fetch the WordPress posts you can write post in the post_type parameter. If you want to fetch the custom post type you can pass the name of the custom post type like movies or courses, to match whatever your custom post type name is in WordPress.
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 am using the WordPress plug-ins Advanced Custom Fields, and Custom Post Type UI.
I have built a WP_Query to display a particular post type, filtered by a particular custom field value.
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'news_story_type',
'meta_value' => 'release',
'posts_per_page' => 3
) );
I now want to sort the resulting posts by another custom field, date_of_publication rather than use WordPress's menu_order or date. The ACF documentation says to specify orderby and meta_key in the query args.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC' );
But alas, doing so conflicts with the meta_key I've already supplied to filter.
Has anyone encountered this before and found a solution?
Try using meta_query
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 3,
'meta_query' => array(
array('key' => 'news_story_type', 'value' => 'release')
)
) );
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()
)
)
);