17
votes
Accepted
How to get post content by calling ajax?
First, you should always use the WordPress AJAX methods, not a custom function for that. See AJAX in Plugins in the Codex.
With that practice in mind, you can set up your request like this. Change ...
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,
"...
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');
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
//...
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 ...
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
...
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' =>...
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 ...
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 ...
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/
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 = ...
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(
...
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 ...
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 ...
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 = [
'...
6
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 ...
5
votes
Use of caller_ get_ posts
Notice:
caller_get_posts is deprecated since version 3.1!
Use ignore_sticky_posts instead.
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',
...
5
votes
Accepted
Get posts by language in Polylang plugin
If you use the query_post function, I think you can just add the 'lang' parameter:
$args_menu = array (
...
'lang' => 'it',
...
);
See : http://wp.primacom.cz/en/enplugins/how-to-display-...
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 ...
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 ...
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' => ...
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' =...
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 ...
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 ...
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;
...
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 ...
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 ...
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
);
$...
4
votes
Check return value of get_posts
Using a simple empty function.
$wpse_posts = get_posts( $args );
if ( ! empty( $wpse_posts ) ) {
//Do the loop
} else {
//No post found
}
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
get-posts × 806wp-query × 169
posts × 117
custom-post-types × 99
query-posts × 80
loop × 76
categories × 63
php × 58
query × 48
custom-taxonomy × 34
custom-field × 31
pagination × 29
meta-query × 28
post-meta × 25
functions × 23
pages × 21
plugin-development × 20
terms × 20
attachments × 18
array × 17
order × 17
images × 16
advanced-custom-fields × 16
taxonomy × 15
tags × 14