Ordering posts in get_posts

You can set the orderby to post__in. Here is a superior post loop: $args = [ ‘post__in’ => [ 1, 2, 3, 4 ], ‘orderby’ => ‘post__in’, ]; $q = new \WP_Query( $args ); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); ?> <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>> <?php the_title(); … Read more

Can I use numberposts=-1 and offset together when using get_posts()?

I believe the issue is that the underlying MySQL query requires a LIMIT clause to go with the OFFSET clause. (I tried to verify this about MySQL and found this https://stackoverflow.com/questions/255517/mysql-offset-infinite-rows ). Since MySQL can’t accept those parameters (offset without a limit), WordPress seems to drop the offset argument. It’s still a little hacky, but … Read more

excluding current post from get_posts

You overwrite the value of $post when you output the first set of posts, so it’s no longer the same post object as your single post. You can either wp_reset_postdata() after each loop (which you should do after the last one anyway), or assign $post->ID to some other var before the loop, and reference that … Read more

`get_posts()` ignore my custom post

If we do not specify post types, WordPress searches only ‘posts’ $actus_new = get_posts( array( ‘post_type’ => array( ‘cantine’, ‘post’ ), ‘post__in’ => $ids, ‘posts_per_page’ => 5, ) );

var_dump and print_r cause white screen

Actually, memory was exhausted. Someone has copy-pasted an actual 2MB base64-encoded image into the post content, var_dump was trying to output it. Had to delete the image via PHPMyAdmin, because the WP editor won’t open. Once it had been deleted, the problem was gone.

If clauses in get_posts query

only retrieve posts with that meta key: $rand_posts = get_posts(array( ‘numberposts’ => 4, ‘orderby’ => ‘rand’, ‘post_type’ => ‘ansatte’, ‘order’ => ‘ASC’, ‘meta_key’ => ’employee_pic’, )); or maybe use WP 3.1’s meta_query: $rand_posts = get_posts(array( ‘numberposts’ => 4, ‘orderby’ => ‘rand’, ‘post_type’ => ‘ansatte’, ‘order’ => ‘ASC’, ‘meta_query’ => array( array( ‘key’ => ’employee_pic’, ‘value’ … Read more

Secondary Query Is Breaking Main Query

you don’t need to call wp_reset_postdata() for get_posts() because it does not actually modify global variable $wp_query. $posts though is a global variable used by WordPress. Change that to a new name and what you have should work. $args = array( ‘post_type’ => ‘quote’, ‘posts_per_page’ => 1, ‘orderby’ => ‘rand’ ); $quote_posts = get_posts( $args … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)