So, I tried all the "add_filter" things on the whole web to get this right but it doesn't work! It always shows me more than 1 post. What's wrong with my code? Latest WP-Version 4.1.1 and no plugins installed.
Here is the code:
<?php
$sticky = get_option('sticky_posts');
if ( !empty($sticky) ) {
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1
);
$slider_query = new WP_Query( $args );
while ( $slider_query->have_posts() ) {
$slider_query->the_post();
?>
<div>
<!-- here we go -->
</div>
<?php
}
}
?>
Okay, I got it. This parameter is missing. Wordpress ignores "posts_per_page" by default for sticky posts.
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1
);
Wordpress doesn't play nice with posts_per_page and wp_query.
To fix it you have to use get_query_var, like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
And then in your wp_query() you do:
$args = array(
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'paged' => $paged
);
Related
I was searching for this but didn't find the solution. I am getting posts orderby title but some posts need to come first so I added a custom field which meta_key is display_postion and the values for this fields are numbers i.e. 1,2,3. So how do I achieve this, below is my current code.
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby'=> 'title',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged
;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts()) : $wp_query->the_post();
Adding meta_value_num to an orderby array and referencing 'display_position' in the meta_key field should do the trick:
$args = array(
'post_type' => 'products',
'order' => 'ASC',
'orderby' => array(
'meta_value_num' => 'ASC',
'title' => 'ASC'
),
'post_status' => 'publish',
'posts_per_page' => - 1,
'paged' => $paged,
'meta_key' => 'display_position'
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
See documentation for Order and Orderby parameters for WP_Query.
In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
I am trying to make a simple query in wordpress ordered by a meta_value_num of a custom field.
$args1 = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
I want to include the posts that have the custom field empty or null too ordered first or last in the query. How can I achieve that?
A simple and quick solution:
<?php
setup_postdata( $GLOBALS['post'] =& $post );
$posts= get_posts(array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
));
if( $posts ):
foreach( $posts as $post ): ?>
<!-- HTML HERE -->
<?php endforeach;
wp_reset_postdata();
endif; ?>
?>
If you want to get the generated SQL just pass it to the WP_Meta_Query object:
$query_args = array(
'post_type' => 'task',
'post_status' => 'publish',
'meta_key' => 'task_due_date',
'orderby' => 'meta_value_num',
'order' => ASC,
'posts_per_page' => -1,
);
$meta_query = new WP_Meta_Query();
$meta_query->parse_query_vars( $query_args );
$res = $meta_query->get_sql(
'task',
$wpdb->posts,
'ID',
null
);
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'
);
I am querying into database using the following code and it is working fine
<?php query_posts(
'post_type=gallery&posts_per_page='.$gnum.'&paged='.$paged.'&orderby=title&order=ASC'
); ?>
Could you please tell me how to add WHERE NOT with the above query.
(WHERE ID != '400' AND ID!='401')
In wordpress query post add argument post__not_in.
'post__not_in' => array(400, 401)
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'post__not_in' => array(400, 401),
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC'
);
So your query post will be like,
query_posts($args);
Try this...
$args = array(
'post_type' => 'gallery',
'posts_per_page' => $gnum,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post__not_in' => array(6,2,8)
);
query_posts( $args );
<?php query_posts(
'post_type=gallery&posts_per_page='.$gnum.'&paged='.$paged.'&orderby=title&ID='-400,-401'&order=ASC'
); ?>