I’m trying to get only the fields that I need using the get_posts()
function in WordPress. I currently have the following code:
$posts_args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'fields' => 'ids'
);
$post_ids_r = get_posts($posts_args);
This works fine if I only want to get the id. But if I want to get the permalink or the title of the post along with the ids that’s where I’m not sure what to do. I already tried the following:
'fields' => array('ids', 'post_titles')
'fields' => 'ids,post_titles'
'fields' => 'ids,titles'
'fields' => array('ids','titles')
But nothing works, I guess the only one that it recognizes is the ids
field. Is there any other way to do this if its really not possible to do it using get_posts()
? Thanks in advance.
get_posts
passes the heavy lifting off toWP_Query
and if you look at the source of that class you can see that there are only a limited number of options with thatfields
argument. There are only three options in thatswitch
—ids
,id=>parent
, and the default case, everything.You can use the
posts_fields
filter to alter what fields get returned, though it looks like you need to pass'suppress_filters => false
in the arguments in order to get that filter to run. It should look something like this:However, there is a larger problem. The post objects that get returned are created by a call to
get_post
and it doesn’t honor the values passed into the original query and I don’t see a way to change what gets returned either inget_posts
or in theWP_Post
class itself.Have a look to this
It’s a function that mimics get_posts but with the ability to get the fields you desire. Be aware: this function is not get_posts and has 2 great limitations: run only in posts table so taxonomy and meta query cannot be run!
However, the query can rely on all the post fields and on some ‘special’ arguments like
include
,exclude
andsearch
.The good part is this: the fields you are able to retrieve are all the field of the post table. Just pass a list or an array in the
fields
argument.Bonus: passing only one field is returned a one dimensional array of strings or integers (instead of an array of objects).
List of Available args are:
Examples of usage
You can only use
'ids'
or'id=>parent'
for the parameterfields
.If you parse something else it will return all fields (this is default).
However, it would be nice if WordPress could add the following 2 options:
'titles'
and'ids_and_titles'
.I am not aware of a way to parse an array for this parameter. I also think it will never happen, since the limits of the answer given by G. M.
More info:
http://codex.wordpress.org/Class_Reference/WP_Query#Return_Fields_Parameter
Interestingly enough, you can do this with the WP Rest API using the _fields parameter
More info on the API here: https://developer.wordpress.org/rest-api/
Thanks gmazzap for that function!
I was looking for a solution to get some custom fields in one with the get_posts, so I felt free to extend it for custom fields of the postmeta table:
You can use the orderby of that fields too.
A small example:
If you want to display the post title inside a template or in a plugin, you can use:
See wordpress reference: http://codex.wordpress.org/Function_Reference/get_the_title
If you’re using it outside the loop, the $ID is required.
Either way, it will probably be used like this:
You can also use:
the_permalink() – to retrieve post permalink
the_title() – to retrieve post title
These are all wordpress template tags. A full list of template tags you can use are found here:
http://codex.wordpress.org/Template_Tags