Support

Account

Home Forums General Issues Join posts with fields in single query

Helping

Join posts with fields in single query

  • I need to display all posts of a custom type at once, therefore I do something like following:

    `function me_get_all_posts_for_export($post_type, $min_date)
    {
    $posts = get_posts(array(
    ‘post_type’ => $post_type,
    ‘posts_per_page’ => PHP_INT_MAX,
    ‘post_status’ => ‘publish’,
    ‘cache_results’ => false,
    ‘suppress_filters’ => false,
    ‘date_query’ => array(
    ‘after’ => $min_date,
    ‘inclusive’ => true
    )
    ));
    return $posts;
    }`

    Now I want to extend this query to directly load all relations I have instead of having to iterate over all posts and make queries for custom fields one by one (via get_field). Can I somehow tell wordpress to join the custom fields into my query automatically at once?

    Single queries per posts are damn slow…

  • This isn’t part of what you asked, but You can use -1 for posts_per_page to tell WP to get all of the posts

    
    <code>posts_per_page</code> => -1,
    

    But as far as your real question goes, no. WP_Query is not capable of doing joins to other posts through a custom field. For something like this you would need to write your own SQL query and use https://codex.wordpress.org/Class_Reference/wpdb and then write your own code to loop over and display the results.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Join posts with fields in single query’ is closed to new replies.