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?
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 )
OK, thank you. Are you working on the test installation, and do you need me to do anything on my end?
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.
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.
Great. Thank you so much Jamal!
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 🙂
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;
}
Perfect! My issue is resolved now. Thank you!