• Hi, I’m trying to use ccsve_generate_query filter. I’ve a date custom field (added with Toolset) that is exported as a timestamp. So I’m trying to use the filter to convert it to a different format. This is my code:

    function ccsve_generate_query_callback($query_arr) {
    
      if (isset($_REQUEST['wpcf-time-entry-date']) && $_REQUEST['wpcf-time-entry-date']) {
        
        $timestamp = $_REQUEST['wpcf-time-entry-date'];
        $date = date('Y/m/d', $timestamp);
    
        $query_arr['meta_key'] = 'wpcf-time-entry-date';
        $query_arr['meta_value'] = $date;
      }
    
      return $query_arr;
    }
    add_filter('ccsve_generate_query', 'ccsve_generate_query_callback');

    It is not working, in the debug log I don’t see anything.

    Any idea?

    thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Shambix

    (@shambix)

    Did you remember to save the custom field wpcf-time-entry-date in the plugin options first?
    Also check that Toolset doesn’t also have a hidden version of the custom field eg. _wpcf-time-entry-date.

    Thread Starter umbaumba

    (@umbaumba)

    `Hi, thanks for the quick answer.

    Toolset stores the timestamp in wpcf-time-entry-date custom field and Simple CSV/XLS Exporter exports it correctly when it is selected in settings.

    What I need is to edit the custom field value to export it as a readable date with format Y/m/d.

    It seems that the filter is not working at all. Is it possible?

    cheers

    Plugin Author Shambix

    (@shambix)

    I’m not sure that the date format you are trying to use is allowed in WP, hence it may not do anything when used that way.

    Try using day, month and year separately, as shown in the WP documentation.

    Thread Starter umbaumba

    (@umbaumba)

    Hi, you sent me documentation about WP_Query, but I’m not running a query. I assume your hook is getting the field wpcf-time-entry-date value by $_REQUEST[‘wpcf-time-entry-date’]; then I just want to manipulate tha timestamp value it contains with standard PHP date() function. You can check WP documentation about date format here:

    Formatting Date and Time

    Actually, I’m using the same function $date = date(‘Y/m/d’, $timestamp); in a shortcode on the same website and it works fine.

    I’ve activated WP_DEBUG and I’m using log_me() function to display errors (https://www.smashingmagazine.com/2011/03/ten-things-every-wordpress-plugin-developer-should-know/). I don’t see any output of your filter ccsve_generate_query in the debug.log. It seems it is not running.

    What is ccsve_export_returns for?

    cheers

    Plugin Author Shambix

    (@shambix)

    The filter allows you to change/build on the query, therefore it should be built as per WP Query parameters.
    You will not find anything in debug.log if there are no PHP errors.
    If the WP query is malformed, it won’t throw any error, simply it won’t return what you are looking for.

    You can check the file of the plugin containing the filter, here.

    I suggest you form your date query as per documentation and try again.

    Thread Starter umbaumba

    (@umbaumba)

    Hi Shambix, I’ve created a secondary custom field called wpcf-time-entry-date-x, there I’m storing an already formatted date. I selected that field in the plugin settings but then I want to display the values from wpcf-time-entry-date-x under the column wpcf-time-entry-date. So I set up the function:

    function ccsve_generate_query_callback($query_arr) {
    
      log_me('----ccsve_generate_query_callback---');
      
      if (isset($_REQUEST['wpcf-time-entry-date-x']) && $_REQUEST['wpcf-time-entry-date-x']) {
    
        $query_arr['meta_key'] = 'wpcf-time-entry-date';
        $query_arr['meta_value'] = $_REQUEST['wpcf-time-entry-date-x'];
      }
    
      return $query_arr;
    }
    add_filter('ccsve_generate_query', 'ccsve_generate_query_callback');

    It still doesn’t work for me.

    I’ve activated WP_DEBUG and I’m using log_me() function to display errors (https://www.smashingmagazine.com/2011/03/ten-things-every-wordpress-plugin-developer-should-know/). I’ve added a log_me(‘—-ccsve_generate_query_callback—‘); that should display ‘—-ccsve_generate_query_callback—‘ in /wp-content/debug.log. It doesn’t, the only output I get there is:

    [01-Feb-2022 14:18:41 UTC] PHP Notice: Undefined offset: 23 in /wp-content/plugins/simple-csv-xls-exporter/process/simple_csv_xls_exporter_csv_xls.php on line 265

    thanks for your patience

    Plugin Author Shambix

    (@shambix)

    As mentioned previously, the date query needs a specific construct.
    So in your function, you should do something like this:

    $custom_date_query = array(
    array(
                'year'  => 2022,
                'month' => 02,
                'day'   => 02,
    ));
    
    $query_arr['date_query'] = $custom_date_query;

    Not sure what kind of date you are trying to get specifically, but it can’t be an individual post date, since the query happens before knowing the values inside each found post.

    Do check the date query from the WP documentation, there’s several possible examples.

    Thread Starter umbaumba

    (@umbaumba)

    Hi, wpcf-time-entry-date custom field contains numerical timestamp values like “1609977600”. It is exported correctly by Simple CSV/XLS Exporter.

    wpcf-time-entry-date-x custom field contains textual strings like “01/28/2022”. It is also exported correctly by Simple CSV/XLS Exporter.

    Why the below code doesn’t work? It should only change the column name…

    function ccsve_generate_query_callback($query_arr) {
      
      if (isset($_REQUEST['wpcf-time-entry-date-x']) && $_REQUEST['wpcf-time-entry-date-x']) {
    
        $query_arr['meta_key'] = 'wpcf-time-entry-date';
        $query_arr['meta_value'] = $_REQUEST['wpcf-time-entry-date-x'];
      }
    
      return $query_arr;
    }
    add_filter('ccsve_generate_query', 'ccsve_generate_query_callback');

    cheers

    Plugin Author Shambix

    (@shambix)

    Sorry but what is that you’re trying to do exactly?
    Manipulate the post query or change something about the query results?

    Because from your code it seems you are trying to manipulate the query arguments, but what you’re saying sounds like that you actually want to change the query results.

    If you want to change the results after the query has been executed, you need to use this hook ccsve_export_returns.

    You have been trying to use the other hook, ccsve_generate_query, which is used to manipulate the query itself, before the results are even found.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘ccsve_generate_query_callback filter’ is closed to new replies.