I am displaying a gallery of custom post types. When the user clicks a post, instead of redirecting to a single template, I want it to redirect to another page (too easy just change the link since all redirects go to the next gallery), but I want to be able to use the metadata related to the clicked post on a screen later in the process. In example:

I have three pages. Page one is a gallery of photos using wp_query to display all of my "Photos". Page two is a gallery of "Substrates". Page three is a page that will show the meta data about the photo such as Title and Location and beneath it the substrate data such as Material and Color.

When a user clicks a photo on page one they will be redirected to page two to select a substrate. When a user clicks the substrate they will be taken to page three to show them all the meta about their choices.

How do I collect this info to be used on page three? In my working, non-WP example I added an onclick function to every item displayed. When click is detected, I add variables to localStorage and save the values. Then redirect to next gallery. On page three I'd simply call all the variables from localStorage using JS. I think I can implement this in the WP site but should I be saving the post ID and then when I am ready to display the info about the selections I make a db call? What is the best way to achieve this type of result?


One option could be to use data attributes on the entires listed on pages one and two. Then attach a click event that reads the attribute value on click saves it to localStorage. This way you wouldn't need to make an extra db call on page three.

In the loop the data attribute value can come basically from anywhere. E.g. post meta, some post field, taxonomy or taxonomy term.

<?php while (have_posts()) : the_post(); ?>

  <article class="entry" data-my-attribute="save-me-to-localstorage-onclick">
    <!-- gallery image -->
    <a><!-- link to next page/view --></a>

<?php endwhile; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.