-
AuthorPosts
-
December 9, 2022 at 12:53 am #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
JorgeDecember 9, 2022 at 9:55 am #1375633Hey 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,
IsmaelDecember 9, 2022 at 11:46 am #1375651Hi 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
JorgeDecember 9, 2022 at 5:41 pm #1375710Hi 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
JorgeDecember 12, 2022 at 9:09 am #1375910Hi,
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,
IsmaelDecember 12, 2022 at 7:01 pm #1375980Hi 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
JorgeDecember 12, 2022 at 8:15 pm #1375986Hi,
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 -
AuthorPosts
- The topic ‘Implementing pre_get_posts’ is closed to new replies.