Archived topic
New Hooks Are Suddenly Not Working Anymore
19 replies · Started by Abrar Mohi on July 21, 2021
Hi there,
So I use GeneratePress Elements on a website where I created a few headers and hooks, it worked properly. Today I created a new hook to add some content to generate_after_content but it is not showing. There are multiple hooks on that page, all the others I created before show up but not the new ones. It also doesn't show on the WordPress top bar where I can see which elements after active on a page. Do you know what could be the problem?
I have checked that the display rules are set correctly. And I have also tried disabling cache and optimization plugins but it did not help. No errors on PHP logs as well. I am on the new WordPress 5.8.
Thanks a lot!
Hi there,
can you temporarily disable ALL other plugins apart from GP Premium to see if there is a conflict.
This includes and cache or security plugins.
Hi David,
Just tried disabling everything but GP Premium. Still doesn't work.
I have enabled them back online since it is a production site. If you want to test it, the credentials are given. You can log in and temporarily disable everything to test it yourself.
Hi Abrar,
I tested by switching back to parent theme, and the hook works properly:
https://www.screencast.com/t/drYHF7SG
So there must be some custom functions in your child theme that interfere with the elemenet.
Thanks a lot for the hint. I just commented out my custom functions one by one and found the culprit.
Can you tell from a quick look what could be the problem? This function is used to change the number of posts on the front page by -1. It seems to be working but I am not sure why this is conflicting with GP. :(
add_action( 'pre_get_posts', 'query_offset', 1 );
function query_offset( &$query ) {
if ( ! ( $query->is_home() || is_main_query() ) || is_admin() ) {
return;
}
$offset = -1;
$ppp = get_option( 'posts_per_page' );
if ( $query->is_paged ) {
$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
$query->set( 'offset', $page_offset );
}
else {
$query->set( 'posts_per_page', $offset + $ppp );
}
}
add_filter( 'found_posts', 'adjust_offset_pagination', 1, 2 );
function adjust_offset_pagination( $found_posts, $query ) {
$offset = -1;
if ( is_admin() ) {
return;
} else if ( $query->is_home() && is_main_query() ) {
return $found_posts - $offset;
}
return $found_posts;
}
I got the solution from the reference mentioned here: https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/
The function you are adding is quite different than Tom's function here:
https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-372928
Those are WordPress core filters so it wouldn't conflict with GP.
I'd recommend identifying which of the two functions is causing is the issue and go from there.
Someone mentioned there that it was breaking the pagination, that's why I tried the third-party solution a person linked in that post. That worked but conflicted with GP.
So, I went ahead and used Tom's function but it created a few problems:
- GP Elements not showing didn't solve. The only way to make GP Elements work is by removing that piece of code. It looks like when I hook into pre_get_posts, GP Elements do not show up.
- In Tom's code when I set the number of posts to 7 and set the number of posts to 8 in WordPress settings, one post goes missing (the first post of the second page). This problem gets solved when I set 7 in WordPress settings as well. I am guessing it should not be like that. My target is to show 7 posts on the first page and 8 posts on the paginated pages because of the first giant post taking two columns.
This is the code I added:
add_action( 'pre_get_posts', 'change_posts_number_first_page', 1 );
function change_posts_number_first_page( &$query ) {
if ( ! is_main_query() || is_admin() ) {
return;
}
if ( ! $query->is_paged ) {
$query->set( 'posts_per_page', 7 );
}
}
These are WordPress functions so we can't go through the debugging steps for you.
Also, these functions should not be added using GP's element. Try one of these methods instead:
Adding PHP: https://docs.generatepress.com/article/adding-php/
Understood. It is just that when I use your given function (Tom's), the new elements don't show up.
No, I didn't add it through GP Element. It lives on the child theme's functions.php.
I don't see an element called related posts here:
https://www.screencast.com/t/NFwfVUYdc
Oh sorry, it is the one named "test".
"Related Posts" was on the production domain.
Looks like the number 7 in your function is causing the issue for some bizarre reason.
If you want the first page to have 7 posts and the rest to have 8 posts, then try this function:
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
function tu_change_posts_per_page( &$query ) {
if ( ! is_main_query() ) {
return;
}
if ( $query->is_paged ) {
$query->set( 'posts_per_page', 8 );
}
}
Yes, it is really bizarre that any number other than 7 works. :D
According to the function you suggested, It shows 8 posts on the first page creating one new line for the 8th post. That's the thing I am trying to avoid.
In the WordPress readings setting, set it to display 7 posts.
Then my function above should work - I've actually tested that in your test install.
I have set things the way you said.
Readings setting: 7 (https://ibb.co/JB5zP5T)
Functions.php code: 8 (https://ibb.co/zQwrSgy)
Homepage: still shows 8 (https://ibb.co/PMfYvy1)
It doesn't seem to solve the problem, unfortunately.