Show posts and custom post types - php

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.

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 );

Get posts by category and language, using PolyLang

I'm creating a plugin and I already could get the posts by category and by the current language using get_posts() function from WordPress and passing the attribute lang with the pll_current_language() from PolyLang.
$args = array(
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'lang' => pll_current_language()
);
return get_posts($args);
Now, I'm wondering how to get the posts by categories related to the language?
For example, I have the News category for English and Noticias for Spanish. How can I set this automatically?
Something like this:
$args = array(
......
'category' => **current_category_for_this_language**
......
);
return get_posts($args);
Any ideas?
Use pll_get_term and filter by category. In this case '34' is my term ID (gotten by hovering the edit link of the term).
By the way as far as I know get_posts gets only posts in the current page language by default and it gets posts by default sorted by date DESC, so you could omit those from your query I think.
$args = array(
'posts_per_page' => 6,
'category' => pll_get_term(34)
);
return get_posts($args);
Sources
https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
pll_get_term
Returns the category (or post tag) translation
Usage:
pll_get_term($term_id, $slug);
‘$term_id’ => (required) id of the term you want the translation
‘$slug’ => (optional) 2-letters code of the language, defaults to current language
https://codex.wordpress.org/Template_Tags/get_posts
Default Usage
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>

Filter posts by custom field taxonomy

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 );

WordPress WP_Query: Display custom post type based on custom meta value, and also order on another custom meta value

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')
)
) );

Wordpress loop for multiple custom post types

Looking for a way to loop through multiple custom post types and get the 4 most recent posts.
I have the below but doesn't quite work as if you 2 posts of the same post type, it'll show the most recent rather than showing most recent of ALL custom posts.
$args = array('post_type' => array('cs_trainee', 'cs_graduates', 'cs_pros', 'sd_trainee', 'sd_graduates'), 'posts_per_page' => 4, 'orderby' => 'menu_order', 'order' => 'ASC');
$careers = new WP_Query( $args );
Thanks
Just change the query_posts parameters a bit....
query_posts( array(
'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
'cat' => 3,
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 5 )
);
That should take care of it for you.

Categories