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
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.
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
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/
Think i ask questions one at a time:
1:
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?
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.
get_view_query_results() in a foreach is the solution. thanks