I’ve got a local WordPress install that I’ve tried to make customisable by users, so it only shows pages from categories they have selected.
I’ve used the code that I’ve found in the thread How to Set an Individual Homepage for Each User?
and while it works, it breaks the menu on every page, it just dissapears.
Example before:
https://i.stack.imgur.com/dVvLb.png
And After:
https://i.stack.imgur.com/aNqWX.png
Any help would be appreciated. Thank you in advance.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It’s important to add guard clauses to prevent modification of other queries. (For example, menus). We are trying to modify the main query here, so we should bail if this is not the main query.
function my_get_posts( $query ) {
// Bail if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Other guard clauses to ensure we don't affect queries unintentionally.
// Sounds like you are just trying to target the homepage, so we could check for that too.
if ( ! is_home() ) {
return;
}
// We only need to modify the query for logged in users.
if ( ! is_user_logged_in() ) {
return;
}
// Modify the query as needed...
}
add_action( 'pre_get_posts', 'my_get_posts' ); // Note that pre_get_posts is an action, not a filter.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0