get_the_id() not returning ID's of posts











up vote
0
down vote

favorite












I'm trying to dynamically load blocks via AJAX:




  • Blocks with the ID's 1, 2, 3, 4 and 5 are loaded by default on the page.

  • On "Load more" click, five new blocks will show (with the ID's 6,7, 8, 9 and 10).


However, I currently can't even get the ID's of the new blocks to echo out on the page and I'm unsure why? I've tried globalising the var too.



Current approach (ajax-loaders.php):



function ajax_handler(){

check_ajax_referer('load_more', 'security');

$args = json_decode(( $_POST['query'] ), true );

global $postId;
$postId = get_the_id($args);

if( $args->have_posts() ) :
while( have_posts() ): the_post();
echo "the ID of this post is:".$postId;
endwhile;
endif;
die;
}


In console, I get an post error.



And if I do:



echo "the ID of this post is:".$postId;
var_dump($args);



It returns the ID of this post is:NULL.



Unsure on what's happening?










share|improve this question


















  • 1




    What exactly is being $_POSTed? Also, your loop makes no sense inside an ajax call... Did you check the documentation for get_the_id()?? What you need is get_posts and it has a different loop. See stackoverflow.com/a/19598561/1287812
    – brasofilo
    Nov 8 at 11:38

















up vote
0
down vote

favorite












I'm trying to dynamically load blocks via AJAX:




  • Blocks with the ID's 1, 2, 3, 4 and 5 are loaded by default on the page.

  • On "Load more" click, five new blocks will show (with the ID's 6,7, 8, 9 and 10).


However, I currently can't even get the ID's of the new blocks to echo out on the page and I'm unsure why? I've tried globalising the var too.



Current approach (ajax-loaders.php):



function ajax_handler(){

check_ajax_referer('load_more', 'security');

$args = json_decode(( $_POST['query'] ), true );

global $postId;
$postId = get_the_id($args);

if( $args->have_posts() ) :
while( have_posts() ): the_post();
echo "the ID of this post is:".$postId;
endwhile;
endif;
die;
}


In console, I get an post error.



And if I do:



echo "the ID of this post is:".$postId;
var_dump($args);



It returns the ID of this post is:NULL.



Unsure on what's happening?










share|improve this question


















  • 1




    What exactly is being $_POSTed? Also, your loop makes no sense inside an ajax call... Did you check the documentation for get_the_id()?? What you need is get_posts and it has a different loop. See stackoverflow.com/a/19598561/1287812
    – brasofilo
    Nov 8 at 11:38















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to dynamically load blocks via AJAX:




  • Blocks with the ID's 1, 2, 3, 4 and 5 are loaded by default on the page.

  • On "Load more" click, five new blocks will show (with the ID's 6,7, 8, 9 and 10).


However, I currently can't even get the ID's of the new blocks to echo out on the page and I'm unsure why? I've tried globalising the var too.



Current approach (ajax-loaders.php):



function ajax_handler(){

check_ajax_referer('load_more', 'security');

$args = json_decode(( $_POST['query'] ), true );

global $postId;
$postId = get_the_id($args);

if( $args->have_posts() ) :
while( have_posts() ): the_post();
echo "the ID of this post is:".$postId;
endwhile;
endif;
die;
}


In console, I get an post error.



And if I do:



echo "the ID of this post is:".$postId;
var_dump($args);



It returns the ID of this post is:NULL.



Unsure on what's happening?










share|improve this question













I'm trying to dynamically load blocks via AJAX:




  • Blocks with the ID's 1, 2, 3, 4 and 5 are loaded by default on the page.

  • On "Load more" click, five new blocks will show (with the ID's 6,7, 8, 9 and 10).


However, I currently can't even get the ID's of the new blocks to echo out on the page and I'm unsure why? I've tried globalising the var too.



Current approach (ajax-loaders.php):



function ajax_handler(){

check_ajax_referer('load_more', 'security');

$args = json_decode(( $_POST['query'] ), true );

global $postId;
$postId = get_the_id($args);

if( $args->have_posts() ) :
while( have_posts() ): the_post();
echo "the ID of this post is:".$postId;
endwhile;
endif;
die;
}


In console, I get an post error.



And if I do:



echo "the ID of this post is:".$postId;
var_dump($args);



It returns the ID of this post is:NULL.



Unsure on what's happening?







php ajax wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:39









Freddy

3991419




3991419








  • 1




    What exactly is being $_POSTed? Also, your loop makes no sense inside an ajax call... Did you check the documentation for get_the_id()?? What you need is get_posts and it has a different loop. See stackoverflow.com/a/19598561/1287812
    – brasofilo
    Nov 8 at 11:38
















  • 1




    What exactly is being $_POSTed? Also, your loop makes no sense inside an ajax call... Did you check the documentation for get_the_id()?? What you need is get_posts and it has a different loop. See stackoverflow.com/a/19598561/1287812
    – brasofilo
    Nov 8 at 11:38










1




1




What exactly is being $_POSTed? Also, your loop makes no sense inside an ajax call... Did you check the documentation for get_the_id()?? What you need is get_posts and it has a different loop. See stackoverflow.com/a/19598561/1287812
– brasofilo
Nov 8 at 11:38






What exactly is being $_POSTed? Also, your loop makes no sense inside an ajax call... Did you check the documentation for get_the_id()?? What you need is get_posts and it has a different loop. See stackoverflow.com/a/19598561/1287812
– brasofilo
Nov 8 at 11:38














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Simple get_the_id() works within the WP loop and you've put it outside the loop. try:



echo "the ID of this post is:" . get_the_id();


Outside the loop:



global $post;
postId = $post->ID;





share|improve this answer























  • Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
    – Angel Deykov
    Nov 8 at 11:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205992%2fget-the-id-not-returning-ids-of-posts%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Simple get_the_id() works within the WP loop and you've put it outside the loop. try:



echo "the ID of this post is:" . get_the_id();


Outside the loop:



global $post;
postId = $post->ID;





share|improve this answer























  • Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
    – Angel Deykov
    Nov 8 at 11:02















up vote
0
down vote













Simple get_the_id() works within the WP loop and you've put it outside the loop. try:



echo "the ID of this post is:" . get_the_id();


Outside the loop:



global $post;
postId = $post->ID;





share|improve this answer























  • Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
    – Angel Deykov
    Nov 8 at 11:02













up vote
0
down vote










up vote
0
down vote









Simple get_the_id() works within the WP loop and you've put it outside the loop. try:



echo "the ID of this post is:" . get_the_id();


Outside the loop:



global $post;
postId = $post->ID;





share|improve this answer














Simple get_the_id() works within the WP loop and you've put it outside the loop. try:



echo "the ID of this post is:" . get_the_id();


Outside the loop:



global $post;
postId = $post->ID;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 11:06

























answered Nov 8 at 10:58









Angel Deykov

469311




469311












  • Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
    – Angel Deykov
    Nov 8 at 11:02


















  • Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
    – Angel Deykov
    Nov 8 at 11:02
















Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
– Angel Deykov
Nov 8 at 11:02




Actually I don't know what is $args in your case, but get_the_id method works within the WP loop
– Angel Deykov
Nov 8 at 11:02


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205992%2fget-the-id-not-returning-ids-of-posts%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check