Using the MakeIndex (via memoir class), I would like to index a page note by referencing the page number (in the Notes section where the note appears) as well as the note number. Attempts to include \thepagenote somewhere references the number of the last page note in the document.
For example, given the MWE below that has 4 page notes, attempts to index the first page note ends up with an entry in the index referencing the last (fourth) page note.
I noticed that the .ind file still has \thepagenote while page numbers are hardcoded after all processing is done, e.g.
\item First pagenote, \ipn{\thepagenote}{5}.
so I guess I have to figure out some way of expanding \thepagenote sooner. This answer seems closely related, but porting that solution directly did not seem to produce expected results -- a number for \thepagenote is hardcoded into the .ind file, but it is still the last page note.
MWE
\documentclass{memoir}
\usepackage{lipsum}
\newcommand{\ipn}[2]{PAGE #2 NOTE #1}
\makeindex
\makepagenote
\begin{document}
\mainmatter
\part{Part}
\chapter{Chapter}
\index{1st index term}%
\lipsum[1]\pagenote{First pagenote\index{2nd index term|ipn{\thepagenote}}}
\lipsum[2]\pagenote{Second pagenote}
\lipsum[3]\pagenote{Third pagenote}
\lipsum[4]\pagenote{Fourth pagenote}
\backmatter
\printpagenotes
\printindex
\end{document}
MWE like referenced answer
https://tex.stackexchange.com/a/282610/47128
\documentclass{memoir}
\usepackage{lipsum}
\usepackage{hyperref}
\newcommand\pn[2]{\hyperpage{#2}n#1}
\makeatletter
\let\if@nopgnote\iffalse % just for the example
\newcommand{\is}[1]{%
\if@nopgnote
\index{#1}%
\else
\edef\tempnumber{\thepagenote}%
\expandafter\pagenoteindex\expandafter{\tempnumber}{#1}%
\fi
}
\makeatother
\newcommand{\pagenoteindex}[2]{\index{#2|pn{#1}}}
\makeindex
\makepagenote
\begin{document}
\mainmatter
\part{part}
\chapter{chapter}
\lipsum[1]\pagenote{First pagenote}
\lipsum[2]\pagenote{Second pagenote.\is{inside second pagenote}}
\lipsum[3]\pagenote{Third pagenote.}
\backmatter
\printpagenotes
\printindex
\end{document}
