Skip to main content
13 votes
Accepted

Query posts ordering by title, but ignore " and special characters

Try this... $posts = get_posts( array( "orderby"=> "slug", "order" => "ASC", "post_type" => "my-custom-post-type", "posts_per_page" => -1, "...
RiddleMeThis's user avatar
  • 3,767
12 votes
Accepted

How to find out what "Blog pages show at most" is set to

You can get that value with get_option('posts_per_page');
David Sword's user avatar
  • 3,337
11 votes

setup_postdata() does not seem to be working?

global post; does not work with setup_postdata($post); if you want to use the the_title() family of commands etc. It's in https://codex.wordpress.org/Function_Reference/setup_postdata Instead use //...
Ryan Taylor's user avatar
11 votes
Accepted

Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?

Several reasons 1. Filters and Actions By using the standard loop, you execute various filters and actions that plugins rely on. Additionally, you set up the_post correctly, allowing functions such ...
Tom J Nowell's user avatar
  • 59.6k
10 votes
Accepted

Using get_posts vs. WP_Query

Edit: WP Core has changed since the question and answer were written. While I still believe it's better to use WP_Query for other reasons, the suppress_filters rule should be gone from WPCS/VIPCS now ...
Tom J Nowell's user avatar
  • 59.6k
9 votes

get_posts - get all posts by author id

global $current_user; $args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' =>...
kdgilang's user avatar
  • 211
9 votes

When should you use WP_Query vs query_posts() vs get_posts()?

query_posts(): might be used in one and only case if you need to modify main query . It sets a lot of global variables; get_posts(): it is very similar in mechanics and accepts same arguments, but ...
dalveer's user avatar
  • 159
8 votes

When to use WP_query(), query_posts() and pre_get_posts

Just to outline some improvements to the accepted answer since WordPress evolved over the time and some things are different now (five years later): pre_get_posts is a filter, for altering any ...
prosti's user avatar
  • 4,293
7 votes

get_post random and order by not working

If you're hosted on WP Engine, you need to manually enable random ordering in your settings or else 'orderby' => 'rand' won't work. https://wpexplorer-themes.com/total/docs/random-order-wpengine/
Gavin's user avatar
  • 404
7 votes

How to show related posts by detecting the current category?

Here is another clean and very flexible option: Put this code in your functions.php file function example_cats_related_post() { $post_id = get_the_ID(); $cat_ids = array(); $categories = ...
Lawrence Oputa's user avatar
7 votes
Accepted

get_template_part in for loop

Your problem is that the variable passed to setup_postdata() must be the global $post variable, like this: // Reference global $post variable. global $post; // Get posts. $posts = get_posts(array( ...
Jacob Peattie's user avatar
7 votes
Accepted

Get the exact SQL query that get_posts() generated

The get_posts() function creates an instance of WP_Query inside the function and simply returns the array of results. This WP_Query instance doesn't override the global $wp_query and is out of scope ...
Howdy_McGee's user avatar
  • 20.7k
7 votes

Get the exact SQL query that get_posts() generated

There is a way to get the SQL query generated by the get_posts() function by using the 'query' filter (since you are not suppressing the filters in you query, the hook will be fired prior to execution ...
Aurovrata's user avatar
  • 1,336
6 votes
Accepted

Is the first item returned by get_posts() always the latest post?

Yes and Maybe Yes: According to https://developer.wordpress.org/reference/functions/get_posts/ the default is ordered by date in a descending order. Maybe: But plugins may change the query through ...
Clemens Tolboom's user avatar
6 votes
Accepted

How to count posts with specific arguments

The easiest way to optimize this query would be to add 'fields' => 'ids' to the arguments. This way only ids of posts will be retrieved from DB (and usually that’s a big change). $args = [ '...
Krzysiek Dróżdż's user avatar
5 votes

Use of caller_ get_ posts

Notice: caller_get_posts is deprecated since version 3.1! Use ignore_sticky_posts instead.
Fad Lee's user avatar
  • 51
5 votes

meta_query: using BETWEEN with floats and/or casting to DECIMAL

As of 3.8 (see track) the precision can be added to the cast type like so: $posts = get_posts(array( 'posts_per_page' => 100, 'post_type' => 'place', 'post_status' => 'publish', ...
SwitzerBaden's user avatar
5 votes
Accepted

Is there a get_post(s) filter that can alter/replace the output completely?

If you look towards the end of the query process (which starts with the pre_get_posts filter), there is a filter called the_posts (not to be confused with the_post). This allows you to modify the ...
cjbj's user avatar
  • 14.6k
5 votes

WordPress ajax get content post id

There are many errors and bad practices in your code. I've fixed most of them. Explaining all those here is not possible. I've written the explanation as comment where they need to. Please read the ...
CodeMascot's user avatar
  • 4,527
5 votes

get posts based on meta value of the author

Get for all users / authors with user meta field. meta1 = true $args = array( 'meta_key' => 'meta1', 'meta_value' => 'true', 'meta_compare' => '=', 'fields' => ...
Anwer AR's user avatar
  • 1,007
5 votes
Accepted

WP_query category__in not working, only pulls from first category

Following Milo's answer, I found another workaround that works and i'm more confortable with it. $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category__in' =...
Ludovic M.'s user avatar
5 votes
Accepted

WP_Query, tax_query and term_meta: How to?

AFAIK there's no way to achieve that within a single WP_Query, so you'll have to first get a list of term_ids which have a different year than the one in question. I think with the following you'll ...
jgangso's user avatar
  • 1,513
5 votes
Accepted

Get posts by meta value with date

Depends what you are getting back from the 'date_event' post-meta field. That's probably the culprit here -- if it's an actual date with a month and day, it's not going to match what you are getting ...
slashbob's user avatar
  • 307
5 votes

How to retrieve a value from get_posts()?

Each post is an object, which changes the syntax you need to use to access the post name: $arr = get_posts(); $arr = array_reverse($arr); foreach ($arr as $post) { echo $post->post_name; ...
Myles's user avatar
  • 331
4 votes

setup_postdata() does not seem to be working?

2 important things to make this work, use global $post variable to setup the postdata, else the loop functions will not see your custom post object. VERY IMPORTANT: make sure to call ...
Aurovrata's user avatar
  • 1,336
4 votes

get_posts assigned to a specific custom taxonomy term, and not the term's children

Here is complete code hope it helps. Thanks <?php $terms_array = array( 'taxonomy' => 'services', // you can change it according to your taxonomy 'parent' => 0 // If parent => 0 ...
Muddasir Abbas's user avatar
4 votes

Exclude Current Post from Recent Posts Loop

Add this to your $args 'post__not_in' => array( get_the_ID() ) This way you won't have to deal with getting the current post ID and will potentially avoid errors with getting your ID. The ...
Mav2287's user avatar
  • 139
4 votes

get_posts - get all posts by author id

its work by (wp4.9.7) $user_id = get_current_user_id(); $args=array( 'post_type' => 'POSTTYPE', 'post_status' => 'publish', 'posts_per_page' => 1, 'author' => $user_id ); $...
sirmagid's user avatar
  • 141
4 votes
Accepted

Get latest posts from multiple categories

2 choices here, you either need to set the category as an array e.g. $args = array( 'posts_per_page' => 5, 'category' => array(15,16,17,18,19) ); You can't just add the numbers in a list ...
Gareth Gillman's user avatar
4 votes
Accepted

Query all posts where meta value is empty

I think you forgot about the inherit post status. The default one in WP_Query is publish. You should also use = instead of LIKE, to avoid using LIKE '%%' in the SQL query. So try to add this: '...
birgire's user avatar
  • 67.4k

Only top scored, non community-wiki answers of a minimum length are eligible