Custom Fields
Re-Envisioned
Support MB Relationships Retrieve relationships with get_posts()?

This topic contains 1 reply, has 2 voices, and was last updated by  Farheen 2 months ago.

  • Creator
    Topic
  • #12716
    Resolved
    Doug
    Participant

    Is it possible to use get_posts() to retrieve relationships instead of WP_Query()? Does the relationship argument need to be modified in some way?

    I’m running into an issue where either WP_Query() or wp_reset_postdata() is causing problems with other functionality. So I tried switching WP_Query() to get_posts() using the same arguments, but I get an empty array when I do that.

      $args = array(
        'relationship' => array(
          'id' => 'design_to_product',
          'to' => get_the_ID(),
        ),
        'nopaging' => true
      );
      // Query
      $related_designs = get_posts($args);
      print_r($related_designs);
Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #12727

    Farheen
    Moderator

    Hello Doug

    It is not possible to retrieve relationships with get_posts() due to restrictions

    get_posts() is very similar in usage and accepts the same arguments (with some nuances, like different defaults), but returns an array of posts, doesn’t modify global variables and is safe to use anywhere.

    WP_Query is the class that powers both behind the scenes, but you can also create and work with your own instance of it. A bit more complex, fewer restrictions, also safe to use anywhere.

    WP_Query Functions

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.