1

I am receiving a var from a php page like:

$myId = sanitize_key( $_POST['varPostId'] ); $myId = (int) $myId; 

That sets a query:

query_posts(array( 'post__in' => array($myId) )); 

Thing is this is an edit page, which means I will be able to change some input data on the page, but when I click the update button the page looses $myId and the query won't work, which means I get an empty page.

I thought to save the id in localStorage but I still didn't get the right logic nor I know if it's the best way. So I am looking into a php or a js solution.

1 Answer 1

2

You can save it in a session variable.
That way it should be accessible ad long as the session is alive.

// Start session session_start(); // Your code $myId = sanitize_key( $_POST['varPostId'] ); $myId = (int) $myId; // If session value exist don't do anything // If not save $myId as session variable if(!isset($_SESSION["myId"])) $_SESSION["myId"] = $myId; // Use session variable in query query_posts(array( 'post__in' => array($_SESSION["myId"]) )); 

Just keep in mind the session_start. It needs to be started at every page you need to access the variable.

Sign up to request clarification or add additional context in comments.

6 Comments

still getting an empty page, might we quickly go to chat?
Do you use the session variable in the query? I'm using my phone and don't know how to get to chat from here. Edit: I just remembered you need to check if it's set prior to setting the session variable
what is $_SESSION["myId"] ? Why myId and not varPostId?
That is the session variable. You can name it whatever you want. it's an array so use it the way you want.
still a blank page, in the query i then run 'post__in' => array($myId), is that correct or what did you mean with using the session there?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.