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);
Related
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
);
I had a post type music. For which i created custom field Music length. Which store music duration in this format like 01:50
I want to display post on order by Music length. I had use both order by meta_value and meta_value_num. But it does not work. My code to get post is:
<code>
$args = array(
'post_type' => 'music',
'meta_key' => 'length',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 10
);
$pop_posts = new WP_Query( $args );
</code>
My question is how to order the post by its meta value in semicolon format(01:50, 02:46, 03:04, 02:37). So the post with meta value 03:04 comes first then 02:46 and so on! Is there any way?
Use following code
$args = array(
'post_type' => 'music',
'meta_key' => 'length',
'orderby' => "REPLACE(meta_value_num, ':', '')",
'order' => 'DESC',
'posts_per_page' => 10
);
$pop_posts = new WP_Query( $args );
But note that it will be very slow for large datasets as it has to recompute the new string for every row.
i want to show the posts from particular category and after that all remaining posts from other remaining category.
this is my query. what changes to done in $args array for achieving my goal.
note that i can only use one loop.
this is my query.
$args = array(
'post_type' => 'event',
'meta_key' => '_event_end_date',
'meta_compare' => '>',
'meta_value' => $today,
'order' => 'DESC',
'orderby' =>'category',
'paged' =>$paged1,
);
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 );
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.