I'm working with Custom Query in WordPress, Basically, I'm showing 4 most recent posts of a category having ID 4
and my query is as follows:-
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 4,
'cat' => '3',
'paged' => get_query_var('paged'),
);
$q = new WP_Query($args);
This is working fine, but here I have an additional requirement. I want to add sticky posts as well i.e Posts will be stick to the top no matters these posts are recent or old, and total posts_per_page should be always 4 including sticky and recent posts.
e.g If there is no sticky post then I'll show 4 most recent posts and no sticky post. But if there is 1 sticky post then there will be 1 sticky post and 3 most recent posts, a total of 4 posts.
What modification should I made in my Query so that pagination also works fine as well? Thank you.
you need to just select sticky post option form your post.
Then automatically work your scenario.
Example:
if you have selected one sticky post and you have set post per page 4 then wp_query will fetch the sticky post first and then show the other 3 posts.
if you have not selected sticky post and you have set post per page 4 then wp_query will fetch the only 4 posts without a sticky post.
It is WordPress default behaviour.
Check this code
$sticky = get_option( 'sticky_posts' );
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 4,
'cat' => '3',
'paged' => get_query_var('paged'),
'post__in' => isset( $sticky[0] ) ? $sticky[0] : array(),
'ignore_sticky_posts' => 1,
);
$q = new WP_Query($args);
Related
How can i loop WordPress post like-
Last one.
3rd last one from below.
As far as I know I can select last and first one by adding order by asc/desc but i cant figure out 2nd one.
Did you try an offset of recent posts with a limit of 1? Like this:
$args = array(
'numberposts' => 1, // only return 1 post
'offset' => 2, // select the third item found
'orderby' => 'post_date', // field for order by
'order' => 'DESC', //DESC for 3th most recent, ASC for third oldest post
'post_type' => 'post', // could be any post type, remove if you want all post types
'post_status' => 'publish' // only show published posts, remove if you want all even trashed
);
$recent_posts = wp_get_recent_posts( $args );
In my template, on the entry.php, I am trying to list the tags associated with a specific post. However, I only want to show tags that have MORE THAN ONE post associated with it.
Here is my query...
$args = array(
'meta_key' => 'ratings_average', // WP-Rating Plugin Rating Value
'orderby' => 'meta_value_num',
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 10,
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
And in the loop, I can list the tags just fine:
<?php if(has_tag()) {
?><label>TAGS: </label><?php // the_tags( '', ', ', '' );
$tags_array = get_tags( $args );
} ?>
...but its shows tags that have only one post.
I'd also like to be able to list the number of posts associated with each tag. The results would look something like this...
Fruits (3), Vegetables (4), Meats (2)
And the tag would be a link to the TAG page, but the number would not be a link.
Can anyone help? Thanks!
I don't know if this is already covered here but i can't find it.
I'm working on a wordpress project with 3 post types: Hotels, Events and restaurants.
They share one taxonomy (zone).
Now what I want is when I create and event with Zone 1, at the bottom of the single event page display: Hotels from Zone 1 and Restaurants from Zone 1.
Is this even possible to do?
Thanks
I think you can do something like this
$args = array(
'posts_per_page' => -1, //return all posts
'post_status' => 'publish', //only published posts
'post_type' => array('hotels', 'restaurants'),
'tax_query' => array(
'taxonomy' => 'zone',
'terms' => 'zone 1'
),
);
$posts = get_posts($args)
hope that at least points you in the right direction
have a look at this WP_Query
Trying to display common posts from multiple post categories using WP_Query and category__and. Following is the query:
$query = array(
'category__and' => array( 'cat-1', 'cat-2'),
'posts_per_page' => 10,
'paged' => $paged
);
$cat_query = new WP_Query($query);
Now in the above case the posts are correctly fetched but the sticky posts are not displayed first.
Following code solves the problem but it doesn't perform "and" of the categories posts.
$query = array(
'cat' => array( 'cat-1', 'cat-2'),
'posts_per_page' => 10,
'paged' => $paged
);
The above query displays the sticky posts first but doesn't perform "and" on category posts.
Is there any way to satisfy the condition of sticky posts first and common posts from multiple categories?
you can use these parameters to display the sticky posts-
$query_sticky = array(
'category__and' => array( 'cat-1', 'cat-2'),
'posts_per_page' => 10,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
'paged' => $paged
);
$query_sticky = new WP_Query($query_sticky);
Once you have these sticky posts you can merge them in the main query-
$query->posts = array_merge($query->posts, $query_sticky->posts);
$query->post = reset($query->posts);
$query->post_count += $query_sticky->post_count;
$query->found_posts += $query_sticky->found_posts;
$query->max_num_pages = $query->found_posts / $query->get('posts_per_page');
the variables like post_count etc. are required to make the wordpress loop work correctly.
I have to mount the blog posts manually, but I'm not sure if this is the correct way to work, It only brings 9 pages, with 4 posts each, but the blog has 83 posts!
<?php
$paged = get_query_var('paged');
$args = array(
'numberposts' => 4,
'offset' => $paged*4,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
?>
Thanks anyway.
Problem is your 'numberposts' is set to 4
Put it at -1 to get all posts:
'numberposts' => -1,
If you don't set numberposts here, WordPress will pull the number of posts from your Dashboard settings (under Settings -> Reading)
The below note is from this codex section.
Note: With use of the offset, the above query should be used only on a
category that has more than one post in it, otherwise there'll be no
output.
So in-order to display all posts, there should be at-least 2 posts in each categories.
You can try Loops to get all posts. Check The Loop in Action also.