Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1375609

    Hi,
    I would like to show only user’s own posts on blog.
    Please find below code I wrote to do this :
    add_action(‘pre_get_posts’, ‘ShowOnlyUsersPosts’);
    function ShowOnlyUsersPosts($query) {
    if ( is_category() || is_archive() || is_single() ) {
    $query->set( ‘author’, get_current_user_id() );
    }
    return $query;
    }

    It is not working.
    Could you tell me what is wrong ?
    Regards
    Jorge

    • This topic was modified 1 year, 11 months ago by liliwow.
    • This topic was modified 1 year, 11 months ago by liliwow.
    #1375633

    Hey Jorge,

    Thank you for the inquiry.

    Looks like you are using the magazine element. You may need to use the avf_magazine_entries_query filter instead of the pre_get_posts hook. Please replace the hook with the code below.

    add_filter("avf_magazine_entries_query", function($query) {
        $query["author"] = get_current_user_id();
        return $query;
    }, 10, 1);
    

    Best regards,
    Ismael

    #1375651

    Hi Ismael,
    Correct. I will use the avf_magazine_entries_query filter instead of the pre_get_posts hook for it as suggested,
    Actually, I’m using either Blog, Magazine, Masonry and PostSlider depending on pages.
    Please let me know if I have to use other specific filter for those elements.
    Many thanks for your help
    Best regards
    Jorge

    #1375710

    Hi Ismael,
    I added following code :
    add_filter(‘avia_blog_post_query’, ‘ShowOnlyUsersPosts’);
    add_filter(‘avia_post_slide_query’, ‘ShowOnlyUsersPosts’);
    add_filter(‘avia_post_grid_query’, ‘ShowOnlyUsersPosts’);
    add_filter(‘avf_magazine_entries_query’, ‘ShowOnlyUsersPosts’);
    add_filter(‘avia_masonry_entries_query’, ‘ShowOnlyUsersPosts’);

    I founded those filters on your website.
    After testing, it seems to work fine now. It’s great.
    Except for blog, category and archive pages where filter is still not working. Any additional and specific filters to use ?
    Please let me know.
    Many thanks for your help. I really appreciate your support.
    Best regards
    Jorge

    #1375910

    Hi,

    Thank you for the update.

    You may need to add the pre_get_posts hook back for the category and archive pages. Did you set the Enfold > Blog Layout > Blog Layout settings to “Grid”?

    Best regards,
    Ismael

    #1375980

    Hi Ismael,
    I did it. I kept the pre_get_posts hook only for the category and archive pages :-)
    Many thanks for your help really helpful.
    Please close this topic.
    Best regards
    Jorge

    #1375986

    Hi,

    Great, I’m glad that you found a solution, and thanks for the update. I’ll close this thread for now then, please open a new thread if you should have any further questions or problems.

    Best regards,
    Rikard

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Implementing pre_get_posts’ is closed to new replies.