Why does my output of get_the_ID() change after a wp_query?
I use ACF.
I have the following code:
$args = array(
"post_type" = "membership",
"posts_per_page" = 1,
'meta_query' = array(
'relation' = 'AND',
array(
'key' = 'kennel',
'value' = '"' . $toReturn["request"]["groupId"] . '"',
'compare' = "LIKE"
),
array(
'key' = 'user',
'value' = $toReturn["userId"],
'compare' = "="
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query-have_posts() ) {
while ( $the_query-have_posts() ) {
$the_query-the_post();
$mid = get_the_ID();
$totalRuns = uc_totalRuns($mid);
$unprocessedRuns = uc_unprocessedRuns($toReturn["request"]["groupId"], $toReturn["userId"], "registered"); // !-- Changes get_the_ID()
if ($totalRuns == 0 $unprocessedRuns["unprocessed"] == 0) {
wp_delete_post( $mid );
$toReturn["success"] = true;
}
}
}
wp_reset_postdata()
After calling uc_unprocessedRuns()
, the value that is returned by get_the_ID() is different.
This is uc_unprocessedRuns()
:
function uc_unprocessedRuns($groupId, $memberId, $type) {
$toReturn["unprocessed"] = 0;
//Get unprocessed runs
//This call returns a post with the ID that is later returned by get_the_ID() in the calling code.
$args = array(
"post_type" = "run",
'posts_per_page' = -1,
"meta_query" = array(
'relation' = 'AND',
array(
'key' = 'kennel',
'value' = '"' . $groupId . '"',
'compare' = "LIKE"
),
array(
'key' = 'processed',
'value' = 0,
'compare' = "="
),
)
);
$the_query = new WP_Query( $args );
$runIds = array();
if ( $the_query-have_posts() ) {
while ( $the_query-have_posts() ) {
$the_query-the_post();
$runIds[] = get_the_ID();
}
}
$toReturn["runIds"] = $runIds;
wp_reset_postdata();
if (count($runIds) 0) {
if ($type == "unregistered") {
$args = array(
"post_type" = "attendant",
"meta_query" = array(
'relation' = 'AND',
array(
'key' = 'type',
'value' = 'unregistered',
'compare' = "="
),
array(
'key' = 'unregistered_member',
'value' = '"' . $memberId . '"',
'compare' = "LIKE"
),
),
);
}
else {
$args = array(
"post_type" = "attendant",
"meta_query" = array(
'relation' = 'AND',
array(
'key' = 'type',
'value' = 'registered',
'compare' = "="
),
array(
'key' = 'registered_member',
'value' = $memberId,
'compare' = "="
),
),
);
}
$mmq = array(
"relation" = "OR"
);
foreach($runIds as $runId) {
$mmq[] = array(
"key" = "run",
"value" = '"' . $runId . '"',
"compare" = "LIKE"
);
}
$args["meta_query"][] = $mmq;
$toReturn["args"] = $args;
$the_query = new WP_Query( $args );
if ( $the_query-have_posts() ) {
while ( $the_query-have_posts() ) {
$the_query-the_post();
$toReturn["unprocessed"] += 1;
}
}
wp_reset_postdata();
}
return $toReturn;
}
The value returned by get_the_ID() in the calling code is a value that comes from the query that is called with the arguments directly beneath "//Get unprocessed runs".
Am I missing something obvious? Am I making a newbie mistake?
How does uc_unprocessedRuns()
change what get_the_ID() returns in the calling code?
Topic advanced-custom-fields get-the-id wp-query Wordpress
Category Web