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
);
Related
I would like to sort my post and tried nearly everything to make it work.. but no luck :-(
In my loop is a custom post type called deal and expired deal (meta_value through ACF) posts. I want to show the normal posts first and then the expired posts.
This is my code so far:
$args = array(
'posts_per_page' => -1,
'post_type' => 'deal',
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => array($not_in),
);
Any ideas how I can put the "expired" posts behind the normal posts?
What you want to do is set orderby to meta_value and meta_key to your custom field.
$args = array(
'posts_per_page' => -1,
'post_type' => 'deal',
'meta_key' => 'YOURCUSTOMFIELDHERE',
'orderby' => 'meta_value',
'order' => 'DESC',
'post__not_in' => array($not_in),
);
If the DESC order is the wrong direction you can switch it to ASC.
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.
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);
I have a custom post type called 'events' and I am to display the first 4 more recent events in event date order on the home page. I have got the events to show on the home page however the ordering doesn't seem to be working (see below the example). It seems to be ordering in its own way.
$args = array('post_type' => 'events', 'meta_key' => 'event-date', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1);
$events = new WP_Query( $args );
That is my code and here are the results (the dates) I get back.
16/04/2014
16/05/2014
19/03/2014
25/02/2014
27/02/2014
28/02/2014
As you can see, this is not ordering by ASC so what have I done wrong?!
Thanks in advance
Try to use meta_value_num instead of meta_value in orderby parameter. Use following code:
$args = array(
'post_type' => 'events',
'meta_key' => 'event-date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1
);
$events = new WP_Query( $args );
I am using Wordpress. I have the following query to fetch data from database and it is working perfectly
$args1 = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__in' => array(400, 403),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args1);
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//And then some other code to display data
Using 'post__in' => array(400, 403),in the above query I am fetching the rows where ID='400' AND '403' . So when I echo, I get to see only two information.
Now, what I am trying to achieve is to fetch all data from the table but when I display the information I want to get the row where ID is 400 at first then 403 and then rest of the rows based on 'orderby' => 'title', AND 'order' => 'ASC'
Could you please help with the query?
Thanks
Edit
$args2 = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__not_in' => array(400, 403),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args2);
Not sure if this will work within the query_post argument, but it is supplementing valid SQL in to it. Try:
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'case when ID in (400,403) then -1 else title end, title',
'order' => 'ASC'
);
query_posts($args);
That is not going to be possible using the Wordpress query structure like you are doing. One possible solution is to do a second query looking for results not in (400, 403) and simply add this array of results to the end of your first array.
In order to do this, you should probably use get_posts() instead of query_posts() so that it doesn't alter the main loop.
$array = get_posts($args1);
$array = array_merge($array, get_posts($args2);
I suppose you could try doing the following:
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'ID = 400 DESC, ID = 403 DESC, title',
'order' => 'ASC'
);
query_posts($args);
...
Let me know how this goes.