Skip Navigation

[Résolu] How to make "get_view_query_results" Filter View by Post ID's Passed by Shortcod

This support ticket is created Il y a 1 an et 5 mois. 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 2 réponses, has 2 voix.

Last updated by Dave Il y a 1 an et 5 mois.

Assisted by: Luo Yang.

Auteur
Publications
#2471933

Tell us what you are trying to do?
Perform calculations on view data and then present it on the front end as summaries.

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

I have working code that will take a View's output and then allow me to calculate summaries and totals based on the data in the View, which can then be displayed via shortcode. This works perfectly and the code is able to handle different views of the same type so I can adjust what is being displayed.

The issue I've run into is the get_view_query_results function doesn't take the fact that the second View has a filtered list of post ID's being passed to it via shortcode into consideration. Now this makes sense as it's being passed like this:

[wpv-view name='summary-of-enrolment-report' ids='[non_deposit_orders]']

And I'm sure I need to pass this list via the $args parameter of get_view_query_results. But nothing I do seems to work and the documentation is uselessly vague on how this actually works, simply saying "Optional array of attributes to pass to the View, like shortcode attributes when using the wpv-view shortcode."

Here is the main code:

function get_report_totals( $atts ) {

  //Set the default $atts values
  $defaults = array(
    'view'		=> 	3112, //Accounts Receivable View
    'summary'	=>	'paid',
    'output'	=>	'total'
  );

  //Apply default atts if none have been set
  $atts = shortcode_atts( $defaults, $atts );

  //Get the results of the View query object
  $posts = get_view_query_results( $atts['view'] );

  //Set an initial counter variable
  $count = 0;

  switch( $atts['summary'] ){
    case 'paid':
      $status = array( 'wc-completed' );
      break;
    case 'outstanding':
      $status = array( 'wc-pending', 'wc-deposit-paid' );
      break;
    case 'partial':
      $status = array( 'wc-deposit-paid' );
      break;
    case 'unpaid':
      $status = array( 'wc-pending' );
      break;
    default:
      $status = array( 'wc-completed' );   
  }

  foreach( $posts as $post ){

    if( in_array( $post->post_status, $status ) ){
      if( $atts['output'] == 'total' ){
        //Get the WC_Order object for the current order in the loop
        $order = wc_get_order( $post->ID );
        //Add order total to the running total
        $count = $count + $order->get_total();
      } elseif( $atts['output'] == 'count' ){
        $count = $count + 1;
      }
    }
  }
  return $count;
}
add_shortcode( 'report_totals', 'get_report_totals' );

I've tried modifying the View call based on this support thread:
https://toolset.com/forums/topic/big-problem-with-view-2/#post-1227849

Like so:

  $args = array(
    'ids' => '[non-deposit-orders]'
    );

  //Get the results of the View query object
  $posts = get_view_query_results( $atts['view'], NULL, NULL, $args );

But this returns 0 every time. Without this post ID filter (which returns a comma-separated list), the calculations are run on every order on the system, not just the relevant ones. But I can't see how I'm expected to pass this to get_view_query_results.

Could someone please clarify how this is intended to work?

#2473211

Hello,

Please try to modify your custom PHP codes, from:

$posts = get_view_query_results( $atts['view'] );

To:

$non_deposit_orders = do_shortcode('[non_deposit_orders]');
$args = array('ids' => $non_deposit_orders);
$posts = get_view_query_results( $atts['view'],  null, null, $args);

More help:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
Arguments:
$args - Optional array of attributes to pass to the View, like shortcode attributes when using the wpv-view shortcode.

#2473361

I'm so sorry, I can't believe I missed to do_shortcode function! Being too close to a problem for too long it seems.

Although I did mention in detail that I'd been reading the get_view_query_results documentation page and that the explanation is fairly vague and no examples are provided, so telling me to look there again is pretty redundant.

Either way, the mistake was entirely on my end, I apologise for the ticket, and thank you for pointing out what I missed.

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