Skip Navigation

[Resolved] View search and result how to get result to a file

This thread is resolved. Here is a description of the problem and solution.

Problem:

I want to get all fields from the view and put them into a textfile.txt

Solution:

It needs custom codes, I suggest you use Views function get_view_query_results() to get all the view's result, then put the data into your file.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

This support ticket is created 6 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 6 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1087107
Skærmbillede 2018-08-19 kl. 20.48.19.png

Tell us what you are trying to do?
i want to get all fields from the view and put them into a textfile.txt
tried to use this code with one variable wpcf-sumafalletal

add_action( 'wpv-after-display-post', 'get_all_fields_on_view', 10, 2 );

function get_all_fields_on_view( $post, $view_id ) {
   $adgang = 0;
    if ( $view_id == 2537 ) {
        $adgang = 1;
        if ($adgang == 1) {
            $myfile = fopen("testfile.txt", "w");
            $txt = get_post_meta( $post->ID, 'wpcf-sumafalletal', true ) ."\n";
            fwrite($myfile, $txt);
        }
        
        if ($adgang>1){
            fclose($myfile);
            $adgang = 0;
        }
    }
}

I only get the first record. I'm not sure how toolset syntax are in wpv-after-display-post or if it is the right one.
Any better solutions?
Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1087394

Hello,

It needs custom codes, I suggest you use Views function get_view_query_results() to get all the view's result, then put the data into your file, see our document:

https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
Returns a result of a query filtered by a View as an array of post objects.

click the link "Usage examples", you will see an example PHP codes.

#1087426
Skærmbillede 2018-08-20 kl. 12.08.34.png

Thanks have been looking at it before.
As a newbie to toolset i'm, unsure which action/filter i shall use, do toolset have any syntax for where to put it?
Like add_action( 'wpv-after-display-post', .....) or is not wpv-after-display-post ;
the code you mentioned:
[php]
// Get the results of the View with ID equal to 2537 and output the post titles
add_action( 'wpv-after-display-post', 'my_count_on_views', 10, 2 );
// Define our callback
function my_count_on_views( $post, $view_id ) {
if ( $view_id == 2537 ) {
$theoutputfile ="testfile.txt";
// Get the results of the View with ID equal to 2537 and output the post
$filtered_posts = get_view_query_results( 2537 );
foreach ( $filtered_posts as $filtered_post ) {
//$one = get_post_meta($post_id,'wpcf-talet', true);
//$two = get_post_meta($post_id, 'wpcf-talto', true);
//$arr=array_merge($arr,array(""=>""));
}

ob_start();
echo '<pre>';
print_r($filtered_posts);
$outputBuffer = ob_get_contents();
ob_end_flush();
file_put_contents($theoutputfile, $outputBuffer);

}
}
>/code>
Give me post data but not post_meta data is it possible to get all that in same file?
Another thin is that this filter run every time i even get there from the menu! Is it possible to make a button in view that trigger the function above?
Thanks and kind regards

#1088189

Q1) Give me post data but not post_meta data is it possible to get all that in same file?

I assume you are going to append the data to the same file, please setup the parameter "flags" as "FILE_APPEND" in function file_put_contents(), for example:
file_put_contents($theoutputfile, $outputBuffer, FILE_APPEND);

More help:
hidden link

Q2) Another thin is that this filter run every time i even get there from the menu! Is it possible to make a button in view that trigger the function above?

No, there isn't such a built-in feature within Toolset plugins, it needs custom codes, here is my suggestion
1) you can setup the button with some HTML codes, and display it in your view's loop section.
2) When user click the button, setup some JS codes, use AJAX submit the post ID parameter
hidden link
3) In server side, create a custom PHP function to register you own AJAX action:
https://codex.wordpress.org/AJAX_in_Plugins
4) in this PHP function put the data into the file.

And the custom codes is out the range of Toolset support:
https://toolset.com/toolset-support-policy/
you can check it with our experienced contractors:
https://toolset.com/contractors/

#1088226

Think i ask questions one at a time:
1:

get_view_query_results() 

seems that it only get an array of post records. How do i get Post_meta? Is it in foreach loop and then insert

 $one = get_post_meta($post_id,'wpcf-talet', true); 

or is there an better syntax in toolset?

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/split-view-search-and-result-how-to-get-result-to-a-file/

#1088228

For the new question, please check the new thread:
https://toolset.com/forums/topic/split-view-search-and-result-how-to-get-result-to-a-file/

This will help other user to find the answers.

#1096411

get_view_query_results() in a foreach is the solution. thanks

#1096498

You are welcome