Hi @alexgff,
could you please explain the need behind your request? I mean, we do understand what you are asking for and I guess there is no reason for us not to add it, but we would like to know why you need to filter returned posts. Maybe it is something other users might find useful. And maybe it can be achieved by adding some option.
Cheers!
This is useful for users who are using multilingual plugins to build multilingual sites. You get posts from the DB directly, and now there is no way to get posts only for the current language. Otherwise you should add support for some multilingual plugins, but adding filter is the smarter way.
Hi @alexgff,
Noted! Understanding now your need, wouldn’t it be better to allow you to filter query arguments? (can’t tell now how the multi-lingual plugin you use works, but I guess it might make us of some custom postmeta field?).
Or if you mean to use the filter to run your own query, wouldn’t it be even better to add a filter at the beginning of the function to allow you to short-circuit it so our query is not run at all, saving resources?
We have already added the filter you requested at the end of the function, but, again, just trying to better understand your need to try to provide a better and more flexible solution.
Hi @molongui
> I guess it might make us of some custom postmeta field?
No.
Fields ‘post_content’, ‘post_title’ and etc contains multilingual values.
Why so? For example, in this case, the custom filter has the ability to search for a substring in any language and return a value corresponding to the search language.
> Or if you mean to use the filter to run your own query, wouldn’t it be even better to add a filter at the beginning of the function to allow you to short-circuit it so our query is not run at all, saving resources?
Most often (9 from 10), it becomes necessary to filter the already received data from the original function, and not create your own query. But, if the developer assumes that a custom query to the DB is possible, then he can add a filter like molongui_authorship_get_author_posts_before with returning no null value to short-circuit.
Hi @alexgff,
OK, following your request, we have added two new filters to the get_posts function. They will be available since version 4.2.0, which is due to be released in a few days.
New filters are:
1) One at the beginning of the function, so you have the option to short-circuit it:
/**
* FILTER HOOK
*
* Allows third-party to filter returned posts list before it is generated.
*
* Passing a non-null value will effectively short-circuit the generation, returning that value instead.
*
* @param array $posts The posts list. Default null.
* @param int $this->id Author ID.
* @param string $this->type Author type. Can be either 'user' or 'guest'.
* @param object $this->author The author's object.
* @param array $parsed_args Array of parsed query arguments.
* @param string|array $args Array or string of function arguments. Default null.
*
* @since 4.2.0
* @version 4.2.0
*/
$posts = apply_filters( 'authorship/author/pre_get_posts', null, $this->id, $this->type, $this->author, $parsed_args, $args );
if ( null !== $posts ) return $posts;
2) Another at the end of the function, filtering returned value:
/**
* FILTER HOOK
*
* Allows third-party to filter returned posts.
*
* @param array $posts Array of retrieved author posts.
* @param int $this->id Author ID.
* @param string $this->type Author type. Can be either 'user' or 'guest'.
* @param object $this->author The author's object.
* @param array $parsed_args Array of parsed query arguments.
*
* @since 4.2.0
* @version 4.2.0
*/
$posts = apply_filters( 'authorship/author/posts', $posts, $this->id, $this->type, $this->author, $parsed_args );
Please note that the get_posts function has been refactored, so you should take a look at it again.
We hope this is just what you needed. If it wasn’t, please feel free to re-open this ticket and let us know.