0

This is my code

<?php
$usersfollowing=1;
echo($usersfollowing);

            add_action( 'elementor/query/my_custom_filter', function( $query ) {
    $query->set( 'author', '1' );
} );
?>

It is working fine when I give value '1' after the 'author'. I want to use $usersfollowing to pass value as 1. When I do $query->set( 'author', $usersfollowing ) it does not work.

I want to use $usersfollowing to pass variable value.

ndm
  • 59,784
  • 9
  • 71
  • 110
  • I suspect you need `function($query) use ($usersfollowing) {` to bring the variable into local function scope. – halfer Jul 20 '20 at 08:39

1 Answers1

0
$usersfollowing = 1;
echo($usersfollowing);

add_action('elementor/query/my_custom_filter', function ($query) use ($usersfollowing) {
    $query->set('author', $usersfollowing);
});

As pointed out in the comments by Nico Haase: In PHP, what is a closure and why does it use the "use" identifier?

meewog
  • 1,690
  • 1
  • 22
  • 26