• jtoney7852

    (@jtoney7852)


    I’m working on a task where I’m create a database. I’m using tablepress for the data which isn’t the problem.

    I’m using the code below to allow me to use a shortcode on each post so I can do some filtering. Problem is the filtering isn’t working.

    function dynamic_tablepress_by_slug_shortcode($atts) {
    $atts = shortcode_atts(
    array(
    'table_id' => '',
    'filter_column' => '0',
    ),
    $atts,
    'dynamic_table'
    );

    $slug = get_post_field('post_name', get_post());

    if (!$slug) {
    return '<p><strong>Error:</strong> Could not determine post slug.</p>';
    }

    $shortcode = '[table id="' . esc_attr($atts['table_id']) . '" filter="' . esc_attr($slug) . '" filter_columns="' . esc_attr($atts['filter_column']) . '" datatables_sort="false"]';

    return do_shortcode($shortcode);
    }
    add_shortcode('dynamic_table', 'dynamic_tablepress_by_slug_shortcode');

    However, I still get the entire unfiltered table. I’m using this shortcode in my post.

    [dynamic_table table_id="1" filter_column="0"]

    Any idea what may be the cause of the problem?

    I’ll crosspost on the tablepress page.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    It seems the issue might be related to how the $slug is being retrieved or passed into the filter. Here are a few steps you can try:

    1. Check the $slug Value: The function get_post_field('post_name', get_post())  is used to fetch the post slug, but it’s worth verifying that it’s working as expected for your posts. Output the value of $slug to confirm it matches the content in your table’s filter column. Make sure the $slug is being correctly retrieved. You can add a debug log to check its value, like this:

      error_log($slug);

      This will help confirm that the slug matches the table filter column content.
    2. Ensure Matching Data: Verify that the slug matches the content of the filter column in your TablePress table exactly. Even small differences like extra spaces or mismatched caps can cause issues.
    3. Test Escaping: Temporarily remove esc_attr() from the shortcode generation to see if escaping is causing the issue. Test the shortcode like so:
    $shortcode = '[table id="' . $atts['table_id'] . '" filter="' . $slug . '" filter_columns="' . $atts['filter_column'] . '" datatables_sort="false"]';

    Try these steps, and let me know how it goes!

    Sandeep Dahiya

    (@sandeepdahiya)

    Hi @jtoney7852

    The filter_columns="0" work only if you have TablePress Row Filtering Module or Extension active which is not included in the base plugin. Without this extension, the table ignores the filter and just shows everything.

    Also, I would recommend you to contact their support forum: https://ww.wp.xz.cn/support/plugin/tablepress/

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problems filtering results on post (using tablepress and code snippet)’ is closed to new replies.