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

  • Creator
  • #12716
    Resolved Doug

    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);
Viewing 1 replies (of 1 total)
  • Author
  • #12727

    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.