Hello everyone, need help. Trying to pass multiple item ids for query from wordpress platform. The ids are taken from post title. My code:
$current_user_id = get_current_user_id();
// Set up the arguments for querying posts
$args = [
'author' => $current_user_id, // Filter posts by the current user ID
];
// Query the posts
$user_posts = get_posts($args);
// Check if the user has posts
if ($user_posts) {
// Collect post titles (numbers) in an array
$post_titles = [];
foreach ($user_posts as $post) {
$post_titles[] = esc_html($post->post_title); // Store the post titles (numbers)
}
$matters = implode(', ', $post_titles); // Comma-separated list of titles (numbers)
} else {
// No posts found, set matters as empty
$matters = '';
}
// Setup API request for Monday.com
$token = 'abc';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
// Create the GraphQL query dynamically
$query = "
query($mattersVar: [String!]) {
items (ids: $mattersVar) {
name
column_values {
column {
title
}
value
}
subitems {
name
column_values {
column {
title
}
value
}
}
}
}
";
// Send the request to the API
$data = @file_get_contents($apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode([
'query' => $query,
'variables' => ['mattersVar' => $post_titles],
]),
]
]));
$responseContent = json_decode($data, true);
$matters is string of numbers: 1234567, 12345678.
$post_titles is an array
No matter I pass $matters or $post_titles to variables, I got the error:
Error in API response: {“errors”:[{“message”:“Arguments are invalid. The argument ‘ids’ should be used”…
$current_user_id = get_current_user_id();
// Set up the arguments for querying posts
$args = [
'author' => $current_user_id, // Filter posts by the current user ID
];
// Query the posts
$user_posts = get_posts($args);
// Check if the user has posts
if ($user_posts) {
// Collect post titles (numbers) in an array
$post_titles = [];
foreach ($user_posts as $post) {
$post_titles[] = esc_html($post->post_title); // Store the post titles (numbers)
}
$matters = implode(', ', $post_titles); // Comma-separated list of titles (numbers)
} else {
// No posts found, set matters as empty
$matters = '';
}
// Store the comma-separated post titles in the $matters variable
echo $matters;
// Setup API request for Monday.com
$token = 'abc';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
// Create the GraphQL query dynamically
$query = '
query ($mattersVar: [ID!]) {
items (ids: $mattersVar) {
name
column_values {
column {
title
}
value
}
subitems {
name
column_values {
column {
title
}
value
}
}
}
}
';
// Send the request to the API
$data = @file_get_contents($apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode([
'query' => $query,
'variables' => ['mattersVar' => $matters],
]),
]
]));
$responseContent = json_decode($data, true);