Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose eg found_posts to QueryBuilder from PostQuery ? #42

Open
jmp909 opened this issue Aug 19, 2022 · 1 comment
Open

Expose eg found_posts to QueryBuilder from PostQuery ? #42

jmp909 opened this issue Aug 19, 2022 · 1 comment

Comments

@jmp909
Copy link

jmp909 commented Aug 19, 2022

would it be possible to expose the PostQuery to the QueryBuilder etc so we can get eg ->found_posts (and any similar data)

currently I have to run a separate WP_Query with my builder's $query->getParameters() just to get the info again but this means querying all the posts that presumably have already been queried

I'm not sure what the syntax for it would be though. I did see it is possible to add elements to the collection outside of the items array,. so maybe accessible that way?

$social_posts = SocialPost::builder()->limit(3)->get();
$found_posts = $social_posts->found_posts;

currently I tack it on to the end of my collection from that WP_Query then call eg

$found_posts = get_found_posts();
$social_posts->found_posts = $found_posts;
dd($social_posts);
^ Tightenco\Collect\Support\Collection {#6769 ▼
  #items: array:3 [▼
    0 => App\PostTypes\SocialPost {[#6565 ▶]}
    1 => App\PostTypes\SocialPost {[#6564 ▶]}
    2 => App\PostTypes\SocialPost {[#6561 ▶]}
  ]
  +"found_posts": 7
}

thanks for any suggestions.
J

@jmp909
Copy link
Author

jmp909 commented Aug 19, 2022

this seems to work in the meantime

$query = (new QueryBuilder)->wherePostType(["news","event"])->limit(3)->as(SocialPost::class);
// or $query = SocialPost::builder()->limit(3);
$post_query = new PostQuery($query->getParameters(), SocialPost::class);
$posts = $post_query->get_posts();
dd(count($posts)." of ".$post_query->found_posts);

shows:

3 out of 10

is there anything that $query->get() will give me that $post_query->get_posts() won't? I guess some Collection functionality? I think I can just do an equivalent with this.

$posts = collect($post_query->get_posts());

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant