Skip Navigation

[Resolved] View Pagination with custom query

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 13 replies, has 2 voices.

Last updated by Puntorosso 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2353049

Hi, I have this code to substitute the query for a view

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 101, 3 ); 
function prefix_show_only_current_author( $query_args, $view_settings, $view_id ) {
    if ($view_id == 80055) {
	    $query_args = array(
	    'post_type'  => 'shop_order',
	    'posts_per_page' => 20,
	    'post_status' => 'wc-completed',
	    'meta_key'       => 'wpcf-ticket-exchange-request-date',
		'orderby'        => 'meta_value',
		'order'          => 'DESC',
	    'meta_query' => array(
	    	array(
	           'key'   => 'wpcf-tickets-exchange',
	           'value' => 'Refund',
	        )
		)
		);
		
	}
	return $query_args;
}

At the same time I set the view to use pagination, but it doesn't work.

I suppose I should integrate the url parameter "wpv_paged" into the query, but I haven't found any hints in the support forum.
Any help is really appreciated.

Thanks. Best

#2353543

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please tell me what is your exact requirement. It seems that you want to filter the posts with post status equal to completed and meta key ticket exchange equal to refund - right?

If yes, could you please tell me what view you created and share admin access details and problem URL.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2353605

Hi Minesh,

Unfortunately, I am not authorized to give administrative access to this site, as it contains sensitive customer data.
I hope you can help me anyway.

The code I sent you earlier is a shortened version of the one we have in place.
You are correctly assuming that we need to filter the posts with post status equal to completed and meta key ticket exchange equal to refund, but we could achieve that also using the View interface.

The reason we use an external query is that we need to search multiple fields using only one parameter.

add_filter( 'wpv_filter_query', 'show_all_refunds', 101, 3 ); 
function show_all_refunds( $query_args, $view_settings, $view_id ) {
    if ($view_id == 80055) {
	    $csearch = $_GET["csearch"];
	    $query_args = array(
	    'post_type'  => 'shop_order',
	    'posts_per_page' => 20,
	    'post_status' => 'wc-completed',
	    'meta_key'       => 'wpcf-ticket-exchange-request-date',
		'orderby'        => 'meta_value',
		'order'          => 'DESC',
	    'meta_query' => 
	    	array(
		    	'relation' => 'AND',
				array(
					'key'   => 'wpcf-tickets-exchange',
					'value' => 'Refund',
				),
				array(
	                'relation' => 'OR',
	                array(
	                    'key' => '_billing_first_name',
	                    'value' => $csearch,
	                    'compare' => 'LIKE',
	                ),
	                array(
	                    'key' => '_billing_last_name',
	                    'value' => $csearch,
	                    'compare' => 'LIKE',
	                ),
					array(
	                    'key' => '_billing_email',
	                    'value' => $csearch,
	                    'compare' => 'LIKE',
	                ),
			)
		);
		
	}
	return $query_args;
}

Everything works, we just can't paginate the results.

Thanks for your support.

#2353703

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure why you keep the attribute: 'posts_per_page' => 20,

What if you try to remove the argument 'posts_per_page' => 20, from your query argument and check if that help you to resolve your issue.

Please check the "Refund" is stored as "Refund" in database or "refund", accordingly you should correct it.

#2353705

I can remove the "post_per_page" argument, but it doesn't make any difference.
The query resolves perfectly, and I can see the results, but I need a way to integrate the parameter "wpv_paged", generated by the Toolset View pagination, into the $query_args parameters.

#2354111

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure why you want to have wpv_paged as URL param, it will be available when you click on the pagination. Are you using any other pagination method, if you can share the reason and why you require wpv_paged as URL param and state your problem that will help me to understand your issue first.

#2354125

I would like to use the Toolset View pagination for this view

[wpv-pagination][wpv-pager-nav-links output="bootstrap" first_last_links="true" previous_next_links="true"][/wpv-pagination]

and the "wpv_paged" url parameter is automatically generated by Toolset.

#2354149

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I know "wpv_paged" URL param is generated by Toolset view's pagination.

The query resolves perfectly, and I can see the results, but I need a way to integrate the parameter "wpv_paged", generated by the Toolset View pagination, into the $query_args parameters.
==>
Why you eant to pass the "wpv_paged" URL param to view's wpv_filter_query's $query_args variable. what you want to achieve exactly by passing the "wpv_paged" that I would like to know first.

Because with you filer the query with status equal to completed orders view will automatically calculate the pagination.

#2354735

I would like to use the view's pagination, but it simply doesn't work, at all.
Not the "Display xx items per page" nor the pagination itself.
And there's no errors in console.

Here the items parameter (5)

hidden link

And here the results (10)

hidden link

So let's pose the question in a different way:

Why the view's pagination is not working with my custom query in the function.php ?

or

How do I make the view's pagination work together with a custom query in the function.php?

Thanks

#2354927

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As you set pagination it should work, Can you please navigate to "Screen Options" button available at to right and check the "View purpose" - check what option is set.

In order to make sure there is no conflict with any theme or plugin you are using:
*** Please make a FULL BACKUP of your database and website.***
Could you please try to resolve your issue by deactivating all third-party plugins as well as with the default theme to check for any possible conflicts with any of the plugins or themes?
- Do you see any difference?

If no, I will require admin access details in order to check whats going wrong with your setup as without knowing we can not be able to guide you in the right direction. If you are not able to share access details to your production site, maybe you should try to setup a staging site and share the stating site access details.

#2357739

Here a screenshot of the full page
hidden link

#2358991

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see the view settings are ok but I could not able to guide you without looking at the site.

Either you can setup a staging site where I can review the issue or you can send me duplicator copy of your site.

Have you tried the following steps?
In order to make sure there is no conflict with any theme or plugin you are using:
*** Please make a FULL BACKUP of your database and website.***
Could you please try to resolve your issue by deactivating all third-party plugins as well as with the default theme to check for any possible conflicts with any of the plugins or themes?
- Do you see any difference?

#2359357

To make it work I had to remove all queries like post type, status .... and leave only the meta query.
This is the final code I am using

add_filter( 'wpv_filter_query', 'show_all_refunds', 101, 3 ); 
function show_all_refunds( $query_args, $view_settings, $view_id ) {
    if ($view_id == 80055) {
	    $csearch = $_GET["csearch"];
	    $query_args['meta_query'] = array(
		    	'relation' => 'AND',
				array(
					'key'   => 'wpcf-tickets-exchange',
					'value' => 'Refund',
				),
				array(
	                'relation' => 'OR',
	                array(
	                    'key' => '_billing_first_name',
	                    'value' => $csearch,
	                    'compare' => 'LIKE',
	                ),
	                array(
	                    'key' => '_billing_last_name',
	                    'value' => $csearch,
	                    'compare' => 'LIKE',
	                ),
					array(
	                    'key' => '_billing_email',
	                    'value' => $csearch,
	                    'compare' => 'LIKE',
	                )
	            )

		);	
	}
	return $query_args;
}
#2359359

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.