0

I'm having an issue with the order WordPress is loading page templates and functions.php.

I have front-page.php which has $_POST['sort-by'] allowing users to sort posts using a drop-down menu.

It's simple enough and works just fine. Now comes the part where I have the ability to "Load more posts" which uses wp_ajax and wp_ajax_nopriv and a function in functions.php to grab the additional posts with an offset of the number of posts shown by default. My AJAX query works just fine, but I'm unable to grab the $_POST data from the front-page.php template within' functions.php to properly sort the posts fetched by the AJAX.

I thought it'd be as simple as just including my same $_POST values within' functions.php so it would use the proper sorting+meta_key, but no matter what I do including attempting to assign a separate variable to $GLOBALS containing the $_POST, I can not for the life of me get it to get the values set in front-page.php in functions.php so it will sort it properly when calling wp_ajax and wp_ajax_nopriv.

I'm assuming this has to do with functions.php being loaded BEFORE front-page.php within' WordPress. I'm not sure if there's an add_action(); of some kind that would allow me to fix my issue here or how I should go about getting it to work as intended. I know it has to be possible though.

EDIT: Here is the function I'm trying to get $_POST data for from my page template frontpage.php

7
  • The request that loads your front page and your subsequent AJAX request are two entirely separate requests with no state maintained between the two, you can't retrieve $_POST data from a different request.
    – Milo
    Apr 5, 2016 at 15:41
  • How can I global the $_POST data to fetch within' my AJAX request then? I tried setting it to a new variable and then globaling that variable with no avail. Pretty stuck on how to accomplish this as I need these posts to sort properly so my AJAX request doesn't pull duplicate posts.
    – Log1x
    Apr 5, 2016 at 18:09
  • post vars in an ajax request are set by javascript. you need to pass whatever data your ajax php needs to operate on the same way you pass the wp_ajax action in your javascript.
    – Milo
    Apr 5, 2016 at 19:23
  • The AJAX request its self isn't processed by a $_POST though. I'm just calling that get_reviews() function as seen above which passes the arguments in the $args array to wp_query and then shows the necessary posts. But, since front-page.php is actually fetching the $_POST variables from the dropdown, it's sorting them differently then what is being done in get_reviews() thus the ajax called posts are not in the proper order. The ajax its self works just fine. If I'm just misunderstanding what you're saying, could I get an example? This has to be possible some way or another.
    – Log1x
    Apr 5, 2016 at 19:30
  • the request that rendered your front page ceased to exist as soon as that page was sent to the browser. all vars set in php are gone. when you make the ajax request, you need to pass all that post stuff yourself with the ajax request, that's where the post vars are populated from in that request. your javascript needs to get the current state of the page, set some vars, and make the ajax request passing those vars. all of that will then be available to your php code triggered by the ajax request.
    – Milo
    Apr 5, 2016 at 19:36

2 Answers 2

0

Assign the value of you $_POST variable to a html element or javascript variable in front-page.php like

//html element

<input type="hidden" value="<?= $_POST['variable']?>" id="post-variable">

or in javascript variable

var postVariable = "<?= $_POST['variable']?>";

Now in ajax call, you can get the value as

var myValue = jQuery('#post-variable').val(); //if you set value to html element

or

var myValue = postVariable; //if you set the value in javascript variable.
4
  • Thanks for the response but I think you misunderstood the issue at hand. I don't need the $_POST variable inside of my Javascript, nor in my input. I need to get the $_POST made to front-page.php inside of functions.php which loads before front-page.php so I can add the values into my wp_query arguments. The actual javascript/ajax call is finished and works just fine. It's sorting within' the wp_query that's causing issues as I'm unable to get the $_POST made to my frontend inside of functions.php
    – Log1x
    Apr 5, 2016 at 12:20
  • How about using 'init' hook? add_action('init', 'my_function'); function my_function(){ if (isset($_POST)){ //your logic here } } Apr 5, 2016 at 12:22
  • I think I'd still be left with the same issue though as my page template "front-page.php" would be loaded after my function, thus my function wouldn't have the up to date $_POST global to pass the arguments into my function, and then into wp_query.
    – Log1x
    Apr 5, 2016 at 12:24
  • Edited my main post to elaborate on the issue.
    – Log1x
    Apr 5, 2016 at 12:30
0

I have figured out how to do this. It was somewhat close to what ankittiwaari suggested, but he didn't give a few key details.

When you pass a variable under data in an ajax call, it automatically makes it a $_POST request assuming you are calling method: 'post' So I did what ankittiwaari said and created <input type="hidden" value="<?= $_POST['variable']?>" id="post-variable"> and then put it into a variable such as var myValue = $('#post-variable').val(); and then passed it into my ajax's data under 'action' as 'sort': myValue and then in my wp_ajax function called $_POST['sort'].

Hope this helps someone in the future with a similar issue while attempting to sort ajax posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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