Skip Navigation

[Resolved] How to get an array of posts from an archive

This support ticket is created 2 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by igorL-3 2 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#2119127

Tell us what you are trying to do? I have created an archive, not a view, for custom posts with filters. I need a function like get_posts() to get an array of posts from this archiveafter the filtering. Is there a function like that or can you direct me to a method.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#2119303

I need a function like get_posts() to get an array of posts from this archiveafter the filtering. Is there a function like that or can you direct me to a method.
I think you could find the filtered results in the global variable $wp_query, in an archive PHP template. Like this:

global $wp_query;
$my_posts = $wp_query->posts;

There's nothing exactly like that in any Toolset API, unfortunately. Most of the Views API filters apply to Views, but not archives:
https://toolset.com/documentation/programmer-reference/views-filters/
The wpv_filter_query_post_process filter would be similar, but only applies to Views.

#2119333

My issue is resolved now. Thank you!

#2119849

Is it too late to ask a followup question?

global $wp_query;
$my_posts = $wp_query->posts;

worked butifly but it only gives the posts on the displayed page not all the posts.
In WP_Query() it is possible to set the 'posts_per_page' => -1 to get all the posts but they would not be filtered. Is it possible to set a parameter so that $my_posts = $wp_query->posts; gives all the posts after the filter?

#2120473

I don't know a simple way to get all the unpaginated results, because the query has already been executed. You can't change it after the fact by setting a parameter. Let me ask my fellow supporters if they know a way to get the unpaginated results using a different approach, I'll let you know what I find out.

#2121215

Sorry, I had no luck getting information about the unpaginated array. There is no public API available for manipulating or extending the existing global query object, so we're a bit limited here in the support available in the forums. The best solution I could come up with is to create a View with identical sorting, filter controls, and Query Filters, but set the Query Filters to respond to shortcode attributes instead of URL params. Apply no pagination to the View. Then you can use the Views API get_view_query_results to get the results of this View, which should be identical to the archive, but without pagination. You would have to access the existing filter URL params from the archive URL using $_GET or something similar, then pass those search filter criteria into the View's Query Filter using the $args argument available for this API:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

$some_archive_param = $_GET['wpv-wpcf-slug']; // get the filter URL parameter values from the current archive URL
$args = array(
'wpcfslug' => $some_archive_param, // set the shortcode attributes for the View using the URL param values from the archive
);
$results = get_view_query_results(1234, null, null, $args); // get the results of the View ID 1234 with filters applied in $args 
#2122047

Thank you Christian for an idea. I did something similar already using WP_Query. Generating a view would run WP_Query anyway. What I did was take the query paramters from $wp_query->query_vars; then cahnge the values of ‘posts_per_page’ and ‘nopaging’ keyed values to -1 and true respectively and ran the WP_Query again. Then got all the posts as per -> posts;
That seems like the same thing since it requires running WP_Query again.

#2122049

Christian is excelent as always
My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.