Wordpress - Get higher hierarchy posts - php

I'm trying to create a menu based on higher hierarchy posts from a custom post type.
The thing is, that I can't find the way to filter hierarchy with get_posts function.
This is what I have so far...
<?php
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'pb_progproy',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = get_posts( $args );
foreach( $posts as $post ){
?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
I know that this will give me all posts regardless of it's hierarchy. What I need is just the ones with higher hierarchy.
Any ideas?
Let's figure this posts structure..
Post 1
Post A
Post B
Post 2
I just want Post 1 and Post 2 to be returned from get_posts function. Is there a way?

If you're using the parent-child hierarchy, you can get parent posts filtering posts where "post_parent = 0"
<?php
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'pb_progproy',
'post_parent' => 0
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = get_posts( $args );
You can read more about post filters here.

Related

get posts order by custom field

here, i want the post order by custom field(created by plugin Advanced Custom Fields), let say the posts order by the orders(custom field), then the categories with greatest custom field value post will appear first, is it available?
here my code is,
$args = array('category' => $cat1->term_id ,
'meta_key' => 'orders',
'orderby' => 'meta_value_num',
'order' => 'DESC' ,'numberposts' => -1
);
$myposts = get_posts( $args );
$i = 1;
foreach ( $myposts as $post ) : setup_postdata( $post );
echo get_field('orders',$post->ID);
$i++;
endforeach;
Can you pass 'post_type' =>'post' in $args array. So it will be like
$args = array('category' => $cat1->term_id ,
'post_type' =>'post',
'meta_key' => 'orders',
'orderby' => 'meta_value_num',
'order' => 'DESC' ,'numberposts' => -1
);
I am assuming that you are passsing post type = post. You can change there it if it's different.

Query to get learndash topics

I have a learndash site with a few courses each with subtopics to make up the course. I need to generate a list of published courses and the subtopics under them. My query for getting the courses:
$args= array('post_type' => 'sfwd-courses',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'ID',
'mycourses' => false);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
endwhile;
This returns the published courses. Now I need to get the subtopics under each course. The post type for the topics is "sfwd-topic" but I dont understand how the 2 are related so I can query it.
Few ways I've done this. This would be the easiest I know of if you're not going into sub-sub-sub topics or anything like that.
You'll need to run a second query for the topics themselves. If you're not going more than 2 levels deep or your sub-topics don't reach into the hundreds for each course, you shouldn't experience any performance issues.
Inside of your while loop, build the array $argsTwo, just like you did $args, and set 'post_type' => 'sfwd-topic'.
$argsTwo = array('post_type' => 'sfwd-topics',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'ID',
);
Then, add another while loop and move through the sub-topics.
while ( $loop->have_posts() ) : $loop->the_post();
$argsTwo = array('post_type' => 'sfwd-topics',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'ID',
);
$loopChild = new WP_Query( $argsTwo );
while ( $loopChild->have_posts() ) : $loopChild->the_post();
endwhile;
endwhile;
This will get the sub-topics for the current post, which is one of your courses. Echo the posts values you need in the format you like.
When your parent loop moves to the next post, it will go through the child loop again to get all of the sub-topics for your new post. Good luck!
EDIT Almost forgot, the new query will get all topics, not related ones without referencing the parent in $argsTwo.
You need to add the parent ID to the 'post_parent' key in $argsTwo, like so:
$argsTwo = array(
'post_type' => 'sfwd-topic',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'ID',
'post_parent' => $loop->ID
);

determine posts per page in second loop wpQuery

I have multiple loops using wp_query() in WordPress.
I want to show the posts from first loop first (post per page=10),
and if there are no posts from first loop show, then show the second loop.
For example, if I only have 8 posts from first loop, the second loop should show 2 posts.
The loop is working properly, but I can't solve the post per page issue. How can I do this? I also need pagination for the remaining posts.
$args = array(
'post_type' => 'event',
'event-categories' => 'featured',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
$args1 = array(
'post_type' => 'event',
'event-categories' => 'abc'
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$loop1 = new WP_Query( $args1);

posts_per_page not working beyond 375 limit

my query doesn't seem to want to function if the post limit set is beyond what it is... i.e. if I have 370 rows of information and I set the posts_per_page to anything beyond that, it won't run the query?!
wp_reset_query();
$args = array(
'post_type' => 'courses',
'posts_per_page' => 370,
'post_status' => 'publish',
);
$loop = new WP_Query( $args );
I've tried using 1000, -1, all sorts, but it only functions if I enter the exact number of rows expected, otherwise the script fails to run at the WP_Query stage so don't have any errors either.
Edit: The actual limit seems to be at 375, after that, nothing executes although there are 386 actual rows that fulfil the criteria.
Any ideas?!!
<?php
$args = array(
'post_type' => 'courses',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 370,
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<?php echo the_content(); ?>
<?php
endwhile;
wp_reset_query();
?>
This MUST work
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'courses',
'post_status' => 'publish',
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) :
while($the_query->have_posts()) :
$the_query->the_post(); ?>
YOUR HTML CODE
<?php endwhile; endif; ?>
I had this exact same problem except the limit for my CTP was 411. Anymore more rows and the query didn't work. Came across a few similar posts and one solution was to increase server resources but then I looked for how to improve wp_query performance. Try this:
$args = array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'update_post_term_cache' => false, // don't retrieve post terms
'update_post_meta_cache' => false, // don't retrieve post meta
);
It worked for me and seems a good option when displaying a CTP index listing page. Discussed here:
https://drujoopress.wordpress.com/2013/06/27/how-to-optimize-wordpress-query-to-get-results-faster/#more-184
http://codex.wordpress.org/Class_Reference/WP_Query#Caching_Parameters

Displaying post with certain key value

I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);

Categories