I am looking for an equivalent of getting a page/post with WP Query or get_posts e.g.:
$args=array(
'category' => 1,
'name' => 'my-page',
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$my_post = get_posts($args);
but I want this for a preview, only thing I have are the preview GET parameters:
?preview=true&preview_id=5&preview_nonce=b0d41a7fdb
The preview_id is actually the ID of the created post, not the id of the preview.
Any ideas?
So I eventually found a possible answer - this should retrieve the latest preview or "revision":
$args = array(
'post_status' => 'any',
'post_parent' => intval($_GET['preview_id']),
'post_type' => 'revision',
'sort_column' => 'ID',
'sort_order' => 'desc',
'posts_per_page' => 1
);
$my_post = get_posts($args);
Related
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 ); ?>
I have 2 custom post types created via theme, rt_book and rt_chapter. Each rt_book has several rt_chapter posts. When I use single-rt_book.php (which should show the contents of a specific rt_book post), how can I filter only the rt_chapter belongs to that specific rt_book being loaded?
My current WP_Query is as follow (which returns all rt_chapter):
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC'
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
I relate these two custom post types by setting the Post Meta of rt_book with an array of rt_chapter post IDs, via function add_post_meta({post ID of rt_book}, 'chapter_orders', {post ID of rt_chapter}) and update_post_meta().
Tried child_of option of WP_Query, but does not affect the results.
Try Like this
$args = array(
'post_type' => 'rt_chapter',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'rt_book',
'value' => get_post_meta(get_the_ID(), 'chapter_orders', true),
'compare'=> 'IN'
),
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
}
Try this. Hopefully this should work.
$tax_slug = get_query_var('taxonomy');
$args = array(
'post_type' => 'rt_chapter', // TODO: Filter specific book.
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'rt_book',
'field' => 'slug',
'terms' => $tax_slug
),
)
);
I'm using sticky posts to allow certain posts to be pinned in a featured post area. I have it working on a development server but when I moved it to the live server it only partially works. If there is a sticky post it displays it. But if there isn't a sticky post it displays nothing and it should display the most recent post. Is there an alternate way to handle this that may work?
$options = array(
'post_type' => post,
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
'status' => 'publish'
);
You can check result of get_option( 'sticky_posts' ) first:
$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
// query options for sticky posts
$options = array(
'post_type' => post,
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
'status' => 'publish'
);
} else {
// query options for the most recent post
$options = array(
'posts_per_page' => 1,
'paged' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
);
}
By default, blog posts can be shown on single page.
i want to customize a front page and display the posts only on the part of my page.
If it's possible, how can i output my latest blogposts onto a custom div or element?
Create custom loop, something like this:
http://www.johnmorrisonline.com/how-to-create-a-custom-loop-in-wordpress-using-wp_query/
So, basically use that WP_Query to get list of post and loop trough it:
http://codex.wordpress.org/Class_Reference/WP_Query
Here is an example how I do it:
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => 6,
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts_array = get_posts( $args );
// print_r($posts_array);
foreach($posts_array as $current_post){
print_r($current_post); // here you have current post values
}
http://codex.wordpress.org/Template_Tags/get_posts
i have a WordPress plugin that i am modifying and i want to echo out the last post title name.
I am not sure if i can use .
How will it know which one is the last one?
any ideas?
Thanks
Use below code:
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => ,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' => ,
'post_type' => 'post',
'post_mime_type' => ,
'post_parent' => ,
'post_status' => 'publish' );
$posts = get_posts( $args );
you can find post's title in $posts.
By changing order in argument list, you can get most recent or most last post.
Ref: http://codex.wordpress.org/Template_Tags/get_posts