Sub post not showing in wordpress admin - php

I am trying to show all the post (including sub post) in wp-admin. but the post which are having parent are not showing in the list of post although the count is giving like 142 post but showing 42 out of 142 since 100 posts are sub posts.
I installed a plugin wp sub post for this where it gave me an option to add a post as parent but not coming in wp-admin.
what can i do for this?
In database it is stored with post_parent id where as the normal have post_parent= 0

try the following code. make sure value of post_parent=-1 to get all posts
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '-1',
'post_status' => 'publish',
'suppress_filters' => true ); ?>
<?php $post_arr=get_posts($args);?>
<?php echo '<pre>';print_r($post_arr);echo '</pre>'?>

Related

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

Output blog posts only onto a part of webpage

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

wp_get_recent_posts not working correctly with appended posts

I want to append some posts to the wp_get_recent_posts function, so they are always retrieved with the normal results, the problem is that I dont get the posts I want to append in the results, am I doing something wrong?
$include = array(1, 2);
$args = array(
'showposts' => 10,
'tag_id' => '123',
'post_status' => 'publish',
'exclude' => $current_id,
'orderby' => 'post_date',
'append' => $include,
);
$entries = wp_get_recent_posts($args, 'ARRAY_A');
If I remove the tag_id argument then the posts I want to append are included in the results but I need to filter the normal results by tag ID, seems that WP also filters the appended posts with the other arguments, is there any workaround?
According to https://codex.wordpress.org/Function_Reference/wp_get_recent_posts there is no 'append' parameter, but there is an 'include' parameter that does what you want.
Try this:
$args = array(
'showposts' => 10,
'tag_id' => '123',
'post_status' => 'publish',
'exclude' => $current_id,
'orderby' => 'post_date',
'include' => $include,
);

Wordpress query for preview?

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

wordpress, how to use get_the_title(ID) to get the title of the last post published in a separate file?

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

Categories