• Resolved Alex Gor

    (@alexgff)


    There is many useful filters in Author class, see includes\class-author.php.
    But there is no way to modify posts list (post_title, post_content, etc)
    in get_posts function.
    I hope you can add it to the next version.
    For example:

    Line 257:
    if ( $fields === 'ids' ) return ( !empty( $data ) ? array_unique( $data ) : array() );
    Change to:

    if ( $fields === 'ids' ) return ( !empty( $data ) ? 
    				apply_filters( 'molongui_authorship_get_author_posts', array_unique( $data ), array($args1, $args2) ) : array() );

    Line 275:
    return ( !empty( $posts ) ? $posts : array() );
    Change to:

    return ( !empty( $posts ) ? 
    				apply_filters( 'molongui_authorship_get_author_posts', $posts, array($args3) ) : array() );
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Molongui

    (@molongui)

    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!

    Thread Starter Alex Gor

    (@alexgff)

    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.

    Plugin Author Molongui

    (@molongui)

    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.

    Thread Starter Alex Gor

    (@alexgff)

    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.

    Plugin Author Molongui

    (@molongui)

    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.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘feature request’ is closed to new replies.