0

I am modifying my Wordpress loop so it only shows posts that were published after the current logged in user was registered. To get the date the user registered I am using the following successfully -

<?php $regdate = date("Y-d-m", strtotime(get_userdata(get_current_user_id( ))->user_registered)); ?>

To modify the loop I am using the below successfully -

    <?php
      function filter_where($where = '') {
      $where .= " AND post_date >= '2010-02-18'";
      return $where;
      }
      add_filter('posts_where', 'filter_where');
      query_posts($query_string);
    ?>

What I need help with is passing the $regdate variable in place of the text date '2010-02-18'. I've tried a few variations but it breaks. I'm sure this is quite simple for anyone PHP savvy... please help!

Scott Eldo
  • 473
  • 11
  • 28
  • Did you try the **global** keyword for your variable ? – Cihan Uygun Mar 18 '16 at 21:11
  • Change the function to `function filter_where($where, $my_date) {` and call it with function `filter_where('', $regdate) {` with this instead of the text `$where .= " AND post_date >= $my_date";` – Steve Mar 18 '16 at 23:01
  • Sorry Steve, I'm a little confused by your answer, would you mind an example? – Scott Eldo Mar 21 '16 at 23:18

1 Answers1

0

As far as i know you could do this in two different basic ways: $GLOBALS and passing function arguments.

$GLOBALS

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

function boo(){
    echo $GLOBALS['foo'];
}
$foo = 'bar';
boo(); // output: bar

Reference: http://php.net/manual/en/reserved.variables.globals.php

Function arguments

Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right.

function boo($foo){
    echo $foo;
}
boo('bar'); // output: bar

Reference: http://php.net/manual/en/functions.arguments.php

Fin
  • 386
  • 1
  • 15
  • Just FYI: I've been told your second solution is better practice than the first. The use of 'custom' global vars is discouraged. – DigiLive Mar 18 '16 at 21:43
  • And why is that discouraged? @dn Fer – Fin Mar 18 '16 at 21:45
  • See http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why To be clear... I'm not saying it's wrong to use them, I'm saying you might want to avoid it. – DigiLive Mar 18 '16 at 21:54
  • I'm a little confused, are you able to give me an example of how I would use this in my code please? – Scott Eldo Mar 21 '16 at 23:21
  • The examples are in my answer, copy the code and see for yourself. – Fin Mar 21 '16 at 23:42