Skip Navigation

[Resolved] Query filter throws an error

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

Problem:
The user was filtering a view using custom code that relies on a 3rd-party plugin function.

Solution:
It turns out that the function returns unexpected results in some cases. The code needs to be updated.

This support ticket is created 4 years, 1 month 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.

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: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by Dan Kitsmiller 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1794781

I'm trying to filter a view using some custom code that Jamal on your support staff helped me with. It queries user meta field that is created with the "simplefavorites" plugin. The code works properly as long as there is data in that field, but if it is empty (in the case of a new user who has not 'favorited" any items yet) it fails with this error:
Notice: Array to string conversion in /var/www/wp-content/toolset-customizations/get-favs.php on line 14

Here are the code snippets involved:

/**
 * Get array of favorite posts (get-faves.php)
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_shortcode( 'my-fav-ids', function($atts){
    $arr = array();
    if(function_exists('get_user_favorites')){
        $arr = get_user_favorites(get_current_user_id(), $site_id = null, $filters = null);
    }
    $res = '';
    if($arr){
        $res = implode(',', $arr);
    }
    return $res;
});
/**
 * Filter Favorites Map Query
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_filter( 'wpv_filter_query', 'pass_favorites_posts_to_view_3539', 10, 3 );
  
function pass_favorites_posts_to_view_3539( $query_args, $view_settings, $view_id ) {
     
    if ( $view_id == 3539 ) { // this is the view id
        $query_args['post__in'] = get_user_favorites(get_current_user_id(), null, null);
    }
     
    return $query_args;
}

How can I check if the array is empty before evaluating it, and return no items found to the view?

#1795641

Hello and thank you for contacting the Toolset support.

You can use PHP built-in functions "empty" or "count". Check this article hidden link

I am not sure what the "simplefavorites" plugin's function will return on the case of a new user, I'll try the following code that checks first, if the $arr variable is actually an array:

is_array( $arr ) && ( count($arr) > 0 )
#1796215

OK, thank you. Are you working on the test installation, and do you need me to do anything on my end?

#1796735

I did not work on your website. I just tried to log in to check this, but I am no longer connected to the site anymore. And the credentials on the previous tickets were automatically deleted after the tickets were resolved.

I wonder if you are able to test the code I suggested? If not, please provide credentials in your next reply and I'll give it a try. Also, let me know how I can test this? What user to use? Any additional screenshots will be helpful.

#1797887

For some reason, the get_user_favorites function from the favorites plugin returned the following results, instead of an empty array(for a new user). Check this screenshot hidden link

Array
(
    [0] => Array
        (
            [site_id] => 1
            [posts] => Array
                (
                )

            [groups] => Array
                (
                )

        )

)

It seems that it is expected for a new user. So, I updated the code of the shortcode to check for this case:

add_shortcode( 'my-fav-ids', function($atts){
    $arr = array();
    if(function_exists('get_user_favorites')){
        $arr = get_user_favorites(get_current_user_id(), $site_id = null, $filters = null);
    }
    $res = '';
    if( is_array($arr) && !is_array($arr[0]) ){ // echo '<pre>'. print_r( $arr, true ) .'</pre>'; exit;
        $res = implode(',', $arr);
    }
    return $res;
});

The errors are not generated anymore.

#1798013

Great. Thank you so much Jamal!

#1798039

So sorry... I spoke too soon. I'm still seeing this error pop up when testing. Looking at the plugin documentation, there is a function available that returns the number of favorites for the current user. Perhaps there is a way to integrate this shortcode so the query filter is only executed if this number is greater than zero? Here is a link to the available functions: hidden link
Sorry to bother you with this, but it seems that I went down a road that exceeds my coding skills and I'm a bit lost 🙂

#1798543

Check the following code, please note that I did not test it:

/**
 * Get array of favorite posts (get-faves.php)
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
add_shortcode( 'my-fav-ids', function($atts){
    $arr = array();
    if(function_exists('get_user_favorites_count')){
		$count = get_user_favorites_count(get_current_user_id(), $site_id = null, $filters = null, $html = false);
		if ( $count > 0)
			$arr = get_user_favorites(get_current_user_id(), $site_id = null, $filters = null);
		else
			return '';
    }
    $res = '';
    if($arr){
        $res = implode(',', $arr);
    }
    return $res;
});


/**
 * Filter Favorites Map Query
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
// Put the code of your snippet below this comment.
add_filter( 'wpv_filter_query', 'pass_favorites_posts_to_view_3539', 10, 3 );
   
function pass_favorites_posts_to_view_3539( $query_args, $view_settings, $view_id ) {
      
    if ( $view_id == 3539 ) { // this is the view id
		$count = get_user_favorites_count(get_current_user_id(), $site_id = null, $filters = null, $html = false);
		if ( $count > 0 ) 
			$query_args['post__in'] = get_user_favorites(get_current_user_id(), null, null);
    }
      
    return $query_args;
}
#1798941

Perfect! My issue is resolved now. Thank you!