Site logo

Archived topic

PHP error with a hook

8 replies · Started by eduard sans on November 5, 2021

Viewing posts 1–9 of 9

Hi there!

So I've been working for the last couple of years with a hook that displayed related posts and everything worked fine so far. Recently I put my site on debugging mode for other reasons and I found this error:

Notice: Undefined variable: post in /home/esans/bundle2.eduardosans.com/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 9 Deprecated: WP_Query has been called with an argument that is deprecated since version 3.1.0. caller_get_posts is deprecated. Use ignore_sticky_posts instead. in /home/esans/bundle2.eduardosans.com/wp-includes/functions.php on line 5495

After breaking down the different possibilities (deactivated plugins, removed custom functions.php and disabled elements) I realized that this hook was triggering the problem. Here's the page where you can find it:
https://bundle2.eduardosans.com/hello-world/

I'm everything but savvy when it comes to php but I tried following the indications quite literally and changed the "caller_get-posts" argument for "ignore_sticky_posts" and that removed already part of the error, but still there's this code showing:

Notice: Undefined variable: post in /home/esans/bundle2.eduardosans.com/wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 9

And here I have no idea what to do.

Here's the code for the hook:

<section class="relatedposts">
	<h4 class="other-posts">
		Otras entradas del blog
	</h4>
<ul class="posts-container">	
<?php
  $orig_post = $post;
  global $post;
  $categories = get_the_category($post->ID);
   
  if ($categories) {
  $category_ids = array();
  foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  $args=array(
  'category__in' => $category_ids,
  'post__not_in' => array($post->ID),
  'posts_per_page'=>3, // Number of related posts to display.
  'ignore_sticky_posts'=>1,
  'orderby' => 'rand',
  'category__not_in' => 0
  );
	
	$my_query = new wp_query( $args );
 
  while( $my_query->have_posts() ) {
  $my_query->the_post();
  ?>

  <li class="relatedthumb">
	  <div class="relatedimg">
    <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(250,200)); ?></a>
	</div>
	  <div class="related-title-box">
    <h4>
			<a class="related-title" href="<? the_permalink()?>"><?php the_title(); ?>
    </a></h4>
	</div>
  </li>
	
  <? }
  }
  $post = $orig_post;
  wp_reset_query();
  ?>

</ul>
	</section>	

Any ideas? Thank you so much in advance guys!

Hi there,

where is that Hook being displayed ? Is it only on single posts ?

Hi there David! these are the settings:

Hook > generate_after_content
Execute PHP > yes
Priority > 10

Display rules > All singular

Although for whatever reason now that I'm checking, it's also displayed in a page(?)

not in just one page, in all pages! :S

All Singular is all single post templates - so thats posts, pages and any single custom post types.
If you just want it on Posts, then change the Display Rule Location to Posts > All Posts.

Then check if its still throwing the error.

oh yeah, that was a rookie mistake (about the "single" thing).

But yep, the error still shows.

Try switching:

$orig_post = $post;
global $post;

to:

global $post;
$orig_post = $post;

You really make things look so simple xD.

Yes, it worked! thanks again!! have a nice weekend David !

Glad to be of help - have a good weekend too!

This archived topic is closed to new replies.