0

I am having a new issue that popped out of nowhere, or at least has not been detected until now. Wordpress or PHP upgrades have happened so I am assuming it's a compatibility issue since it used to work.

Errors listed on debug:

Notice: Undefined index: post in functions.php on line 163

Notice: Undefined index: post_ID in functions.php on line 163

Notice: Undefined index: post_type in functions.php on line 164

Notice: Undefined index: page in single-location.php on line 9

Here are the referenced lines in my functions.php file.

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$post_type = $_GET['post_type'] ? $_GET['post_type'] :get_post_type($post_id);

And here is the referenced line in my single-location.php file:

// single location template

$this_page = $_GET['page'];

I have a website that uses has multiple locations of restaurants and the website has different locations and different data for each location.

I have tried to modify the code using other posts and solutions but UI am unsuccessful in my many attempts.

Any help or guidance is GREATLY appreciated.

Michael G
  • 189
  • 10
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Markus Zeller Dec 20 '20 at 16:34
  • 1
    You may want to take a look at the [NULL coalescing operator](https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op). – Ro Achterberg Dec 20 '20 at 16:35

1 Answers1

2

Given the syntax you tried, you're probably looking for:

$post_id = $_GET['post']??$_POST['post']??123; // 123 == default
$post_type = $_GET['post_type']??get_post_type($post_id);

This syntax features a chained NULL coalescing operator. The first line will assign $post_id if $_GET['post'] is not NULL. If it is, it'll check $_POST['post'] for a NULL value. If it isn't, $post_id will take the value of $_POST['post']. Lastly, if neither were set, $post_id becomes 123.

Same goes for $post_type, first checking $_GET, then 'defaults' to get_post_type()'s return value.

Ro Achterberg
  • 2,504
  • 2
  • 17
  • 17
  • Your solution seems to have taken care of my errors, except one on the actual "location" page that displays that content. the only issue I am having now it the 3rd error, $this_page = $_POST['page']; I apologize in advance for my lack of knowledge. Someone helped build this site almost 7 years ago and it seems I am having trouble fixing these issues up. thanks for your help so far though! – Michael G Dec 20 '20 at 16:52
  • I got it to work. Not sure what happened to the previous not working on the locations page, but you helped me more than you know. Thank you Ro and the others above for the hints. – Michael G Dec 20 '20 at 17:04
  • Glad to know you cracked it. Happy coding! – Ro Achterberg Dec 20 '20 at 18:15