Skip to main content

Pass php variable as item ids for query

  • November 10, 2024
  • 3 replies
  • 9 views

  • New Participant
  • 1 reply

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”…

Any comment is welcome. Thanks in advance.

3 replies

Matias.Monday
Forum|alt.badge.img
  • monday.com Team Member
  • 3512 replies
  • November 10, 2024

Hello there @Sam23 and welcome to the community!

Would you be able to please try using ($mattersVar: [ID!])?

Looking forward to hearing from you 😀

Cheers,
Matias


dvdsmpsn
Forum|alt.badge.img+1
  • Participating Frequently
  • 425 replies
  • November 10, 2024

@Matias.Monday @Sam23 If that’s an array I think it’s more likely:

($mattersVar: [ID!]!)

But always try out your query in the playground first to validate that it’s correct and working.


  • Author
  • New Participant
  • 1 reply
  • November 11, 2024

HI Martias and David, thank you for your reply.

Option 1: When I use ($mattersVar: [ID!]) and $matters for the query. I can only get the first item.

Option 2: If I use ($mattersVar: [ID!]!) and array $post_titles then it gives me error: Error in API response: null

echo $matters : 123456788, 123456789
print_r($post_title) : Array ( [0] => 123456788 [1] => 123456789 )

Below is the code for Option 1:

$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);

Any help would be greatly appreciated.