I have there the code I'm using, and I'm trying to get the posts in the order of the IDs from the $menus array but don't do that, he gave me the posts from the newest to oldest...I have tried to use order with DESC but the array did not change.
$menus = array(105, 54, 111);
$args = array(
'post__in' => $menus,
'orderby' => 'ID',
'order' => 'DESC',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$i = 1;
while ( $query->have_posts() ) : $query->the_post();
do_action('fwp_before_post_content');
get_template_part('extend-helpers/' . $layout);
do_action('fwp_after_post_content');
$i++;
endwhile;
else:
get_template_part('extend-helpers/content', 'none');
endif;
UPDATE:
The posts are from different categories.
UPDATE II:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (105,54,111) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 105,54,111 ) LIMIT 0, 1000"
Why I have ORDER BY wp_posts.menu_order? because in $args I don't have orderby menu_order..
You need to add the "orderby" item in the args you pass WP_Query.
You can find the documentation here
Have you seen the WP_query loop? https://code.tutsplus.com/tutorials/mastering-wp_query-using-the-loop--cms-23031. It uses $args=ASC and DSC and 'orderby ' =>'id' too.
for the first part, you want to use post__in also in orderby line as Juan suggested in his comment. From the documentation: 'post__in' - Preserve post ID order given in the 'post__in' array. So, your code should look like this
$menus = array(55, 53, 57); // I have used mine ids
$args = array(
'post__in' => $menus,
'orderby' => 'post__in',
);
$query = new WP_Query( $args );
For the second part, which is more important, why do you have ORDER BY wp_posts.menu_order. You can find answer also in comments. The query is being altered by something (theme or plugin), check pre_get_posts hook, try switching theme, etc.
I have tested this code and it is working, so it is ordering posts as they are in array $menus.
Related
Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.
I'm trying to get the testimonials (custom post type) in following order.
I can easily retrieve posts using WP_Query class but struggling to create an accordion as shown in the screenshot above.
Try using a custom select query and looping through each post with a post_type of testimonial.
Then loop through result and set WP_Query() class date_query param to an array of the years obtained by the custom select query result.
global $wpdb;
$posts = $wpdb->posts;
//Get all unique years as "years" from posts where post type is equal to testimonials
$sql = "SELECT DISTINCT(YEAR(`post_date`)) as years FROM $posts WHERE post_type = 'testimonials' ORDER BY years DESC"; //Get all post year list by DESC
//Loop through all results and use date_query param https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
$result = $wpdb->get_results($sql);
foreach($result as $rs) {
echo '<h2>'.$rs->years.'</h2>';
$args = array(
'post_type' => 'testimonials',
'post_per_page'=> -1,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(array(
'year'=> $rs->years,
),),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_date().'';
endwhile;
}
}
#user3325126 Great code, thanks :)
Just replace 'post_per_page'=> -1, into 'posts_per_page'=> -1, (missing one s)
It should be
'posts_per_page'=> -1,
SELECT
IF (POSITION('Villa' IN p.post_title )>=1,
SUBSTRING(p.post_title, 7), post_title) AS 'Title',
p.post_title,
p.ID,
p.post_content
FROM wp_posts p INNER JOIN wp_term_relationships tr
ON p.ID=tr.object_id where tr.term_taxonomy_id=4
and p.post_status='publish'
ORDER BY Title ASC;
I can run above query to get data with "wpdb" function like
$wpdb->get_results($query);
But I need the above results to be returned as a Wp_query object as I want to use functions like get_the_excerpt()
The orderby parameter accepts the post__in sorting value as a possible way to sort posts returned. With orderby set to post__in, the posts will be returned in the order they are entered into the post__in parameter.
The following code
args = [
'post__in' =>[3, 1, 2],
'orderby' => 'post__in'
];
$q = new WP_Query( $args );
will return posts 1,2, and 3 in the following order
3,1,2
Try this code,
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status'=>'publish',
'orderby'=>'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => 4
)
)
);
$query = new WP_Query($args);
$query_posts = $query->posts;
var_dump($query_posts);
You can take your WPDB results and use the post functions like this...
global $wpdb;
global $post;
$query = "SELECT
IF (POSITION('Villa' IN p.post_title )>=1,
SUBSTRING(p.post_title, 7), post_title) AS 'Title',
p.post_title,
p.ID,
p.post_content
FROM wp_posts p INNER JOIN wp_term_relationships tr
ON p.ID=tr.object_id where tr.term_taxonomy_id=4
and p.post_status='publish'
ORDER BY Title ASC;"
$result = $wpdb->get_results( $query );
foreach( $result as $post )
{
setup_postdata( $post );
echo "<div>" . get_the_excerpt() . "</div>";
}
Make sure you set the $post global.
I have been trying for a couple of hours to make this work - but for some reason its simply to difficult for me. I have a custom post_type 'house', and I want to find the post_id of my custom post_type with a meta_key and certain meta value.
Lets say i want to find post_id for a house with
meta_key='house_id'
meta_value='231sd1223'
How exactly would i do that with wp->query?
Here you have the query even with a loop. However, querying meta values is making more DB queries, consider looping throught "house" post type and than doing something only if meta_value is equal the house number.
// WP_Query arguments
$args = array (
'post_type' => array( 'house' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => 'house_id',
'value' => '231sd1223',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
global $wpdb;
$results = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '231sd1223.'", ARRAY_A );
I am trying to get the latest posts on my blog. These are being displayed in a little widget in my front page on my website. I am getting a little trouble though. The posts are getting multiplied. http://goo.gl/Q2STSC, Here is my SQL query.
$posts = DB::connection('blog')->select("
SELECT a.post_title AS title, a.post_name AS slug, meta_value AS thumbnail,a.post_content AS contenido, a.post_date AS fecha
FROM wp_postmeta, wp_posts AS a
JOIN wp_posts AS b ON a.ID = b.post_parent
WHERE b.ID = wp_postmeta.post_id
AND meta_key = '_wp_attached_file' AND a.post_status = 'publish'
ORDER BY fecha
LIMIT 0 , 6");
Try this
<?php $args = array(
'posts_per_page' => 6,
'offset' => 0,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
foreach ( $posts_arrayas $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;?>
Please use above code to fetch latest post from you blog.In above code you can see different parameters
posts_per_page : Number of post to get
offset : Starting Post number likeyou do in custom query limit 0[offset],6[posts_per_page]
order : Orderby [ DESC / ASC]
orderby : date [ column on which order clause has to perform]
post_type : post ( type of post] ( if you want to get posts for custom posts like news, then you can pass news as post_type
post_status : publish ( status of post )
Also in foreach you can permalink [ link of the post ] and the_title( title of the post)
For more details you can check here https://codex.wordpress.org/Function_Reference/get_posts