Skip Navigation

[Resuelto] Views adding random criteria that are not defined in the View configuration

This support ticket is created hace 1 año, 7 meses. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 respuestas, has 2 mensajes.

Last updated by Simon hace 1 año, 7 meses.

Assigned support staff: Shane.

Autor
Mensajes
#2090627
Screenshot 2021-06-16 15.33.47.png
Screenshot 2021-06-16 15.33.13.png

We have a Custom Post Type of Journal with various Views on the Content Template e.g.

hidden link

The table of subscription types actually uses two Views to get WooCommerce Products. The Products are set via a One To Many relationship. These Views have worked since we launched the site in January - until the most recent round of Toolset updates. Then they stopped working. Here's an example that is broken:

hidden link

I activated the Views debug output and I can see this is the query:

SELECT   wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND wp_posts.ID IN (8969,8970) AND ( 
  wp_postmeta.meta_key = 'wpcf-publication-date'
) AND wp_posts.post_type = 'product' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC 

The reason that the broken Views are returning no results appears to be that the query is trying to use the Products custom field wpcf-publication date - even though:
a) That field is optional on Products
b) That field is not used ANYWHERE in the View configuration, filtering or output (see screenshots)

What the hell is going on? In the working example, the 'fix' was to set the publication date on the products that meet the View selection criteria. But I shouldn't have to set an arbitrary custom field that I don't need to make a View work when it doesn't depend on that field.

This is not the only sudden problem since the most recent release, by the way, but it's the most obvious bug so I am reporting it.

Resolution needed urgently - I can temporarily fix by adding publication dates to my products, as there only a small number. However, someone with hundred or thousands of posts would be totally stuck!

#2090847

Shane
Supporter

Languages: Inglés (English )

Timezone: America/Jamaica (GMT-05:00)

Hi SImon,

Thank you for getting in touch.

Can you try disabling this plugin below.
- Simple Custom Post Order

Disabling this should resolve the issue. I've seen where Post Order plugins have interfered with our Views queries because they don't limit the scope under which they operate.

Please disable this plugin and let me know if the view data shows up.

Thanks,
Shane

#2092413

I deactivated the plugin you mentioned and it didn't make any difference to the query shown in the Views debug output.

I should also add that we have used that plugin on numerous other websites and never seen any issues like this.

Any other ideas?

#2092571

Shane
Supporter

Languages: Inglés (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

This one would've been the primary suspect because it is known to interfere with the queries. Given that disabling this plugin doesn't have any effect I would recommend disabling all the non-toolset plugins and trying again.

We need to see if the publication date sorting is removed when all the plugins are disabled.

If the issue still remains, please allow me to have admin access to the site as well as a link to the page where I can see the issue.

Thanks,
Shane

#2093389

I deactivated all active plugins except for Woo, Types and Blocks and the query was still problematic

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND wp_posts.ID IN (8969,8970) AND ( 
  wp_postmeta.meta_key = 'wpcf-publication-date' 
  AND 
  ( 
    ( mt1.meta_key = 'wpcf-journal-product-type' AND mt1.meta_value = '5' )
  )
) AND wp_posts.post_type = 'product' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 1

As you can see the meta-key 'publication date' is still in the query - so I dont' think we can blame any plugin. I then switched the theme to the default briefly, at which point the query resolved:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1  AND wp_posts.ID IN (8969,8970) AND ( 
  ( wp_postmeta.meta_key = 'wpcf-journal-product-type' AND wp_postmeta.meta_value = '4' )
) AND wp_posts.post_type = 'product' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title DESC LIMIT 0, 1

So, something in the theme...

I think it must be this snippet in functions.php

/**
 * Adds WooCommerce catalog sorting options using postmeta, such as custom fields
 * Tutorial: <em><u>hidden link</u></em>
**/
function skyverge_add_postmeta_ordering_args( $sort_args ) {

	$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
	switch( $orderby_value ) {

			// Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
		case 'pubdate':
			$sort_args['orderby']  = 'meta_value';
			// Sort by meta_value because we're using alphabetic sorting
			$sort_args['order']    = 'desc';
			$sort_args['meta_key'] = 'wpcf-publication-date';
			// use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
			break;

	}

	return $sort_args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'skyverge_add_postmeta_ordering_args' );
// Add these new sorting arguments to the sortby options on the frontend
function skyverge_add_new_postmeta_orderby( $sortby ) {

	// Adjust the text as desired
	$sortby['pubdate'] = __( 'Sort by publication date', 'woocommerce' );

	return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'skyverge_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'skyverge_add_new_postmeta_orderby' );

The snippet has been in our theme for a while, so I don't know if something in a recent Toolset release has made this more liable to interfere.

Can you see what aspect of the function might be causing the problem?

Thanks!

#2094567

Shane
Supporter

Languages: Inglés (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

Thank you for the update.

Based on the code i see that you're setting the sort order here

 case 'pubdate':
            $sort_args['orderby']  = 'meta_value';
            // Sort by meta_value because we're using alphabetic sorting
            $sort_args['order']    = 'desc';
            $sort_args['meta_key'] = 'wpcf-publication-date';
            // use the meta key you've set for your custom field, i.e., something like "location" or "_wholesale_price"
            break;

What is the original purpose of the code that you've provided? It may need some modification so that it only runs on specific pages.

Thanks,
Shane

#2097063

The code in question allows sorting of Woo product archives via a custom field value (see hidden link). Pretty sure we've used it before without ill effects.

However it's clearly our theme causing the issue, so resolving the ticket now.