• Resolved Andreas 2013

    (@andreas-2013)


    Hi Abhishek,

    I got a problem with your plugin and I hope you can help me.

    I have a custom post type “person” and a custom taxonomy “height”. Your plugin works, the custom fields for the people are written correctly in the custom table and are displayed in the output.
    The URL looks like this: https://example.com/height/185-cm/

    Now I would like to filter the output of the taxonomy into male and female persons. The URL then looks like this:
    https://example.com/height/185-cm/?g=male

    For this I use the WordPress hook ‘posts_where’. This is my code:

    // associate the custom table "mytable" with the custom taxonomy
    add_filter('posts_join','custom_join');
    function custom_join($join){
    global $wpdb;
    $customTable = $wpdb->prefix."mytable";
    if (is_tax('height')) {
    $join .= "LEFT JOIN $customTable ON $wpdb->posts.ID = $customTable.post_id";
    }
    return $join;
    }
    
    // filter the male persons from the output list
    add_filter('posts_where', 'male', 1, 2);
    function male( $where )
    {
    if (isset($_GET['g'])) {
    if ($_GET['g'] === 'male') {
    global $wpdb;
    $where .= " AND gender = 'He' ";
    }
    }
    return $where;
    }

    The output is correctly filtered, only male persons are displayed. However, the custom fields from “mytable” are no longer loaded in the taxonomy output, although I haven’t changed anything in the template. Query Monitor shows me that the plugin is not loaded at all in the URL with the parameter.

    What can the problem be related to?

    Thank you for your help in advance!
    Andreas

    • This topic was modified 1 year, 10 months ago by Andreas 2013.
    • This topic was modified 1 year, 10 months ago by Andreas 2013.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Andreas 2013

    (@andreas-2013)

    I just noticed, that the custom fields in the taxonomy output list have reappeared again after saving the posts again!
    By the way, I’ve imported the posts via plugin WP All Import, maybe the used function is not correct?
    But it’s very strange, that the custom fields are loaded, when I call the taxonomy list without parameter in the URL! That means, that the code actually works…

    • This reply was modified 1 year, 10 months ago by Andreas 2013.
    Thread Starter Andreas 2013

    (@andreas-2013)

    Sorry, but I have to correct my statements!
    To test whether the error is really due to the parameter, I’ve deleted the if-condition of my code:

    if (isset($_GET['g'])) {
    if ($_GET['g'] === 'male') {

    If I now call?https://example.com/height/185-cm/, no custom fields are loaded either!
    They are only loaded, when I’m not using the ‘posts_where’-hook (or if so, after saving post).

    What could that mean?

    • This reply was modified 1 year, 10 months ago by Andreas 2013.
    Thread Starter Andreas 2013

    (@andreas-2013)

    UPDATE:
    The taxonomy doesn’t read from the custom table, but from the wp_postmeta! The single post, on the other hand, correctly displays the custom fields from the custom table!

    Thread Starter Andreas 2013

    (@andreas-2013)

    UPDATE 2:
    The taxonomy READS from the custom table, but only without the ‘posts_where’-hook! With ‘posts_where’-hook the plugin for the custom-table isn’t loaded!

    • This reply was modified 1 year, 10 months ago by Andreas 2013.
    Plugin Author Abhishek

    (@abhisheksatre)

    Hi,

    Can you please provide the sample code you are using to display the taxonomy?

    • This reply was modified 1 year, 10 months ago by Abhishek.
    Thread Starter Andreas 2013

    (@andreas-2013)

    Hi Abhishek,

    sorry for the late reply, but I had to make some more testing first.

    I think it’s probably not necessary to post my custom code. In order to further isolate the cause of the error, I made the tests with the original WordPress templates, without my custom code.

    I worked with a child theme from the Twenty Twenty theme and used the following customized files to output the taxonomy data:
    taxonomy-height.php, content.php and entry-header-height.php.

    Now I copied the original code from the parent theme and used these files: index.php, content.php and entry-header.php. In the functions.php I left only the registration of the CPT ‘person’ and the custom taxonomy ‘height’ as well as the functions described above for the ‘join’, ‘posts_orderby’ and ‘posts_where’ hooks. The result is this:

    The hooks ‘join’ and ‘posts_orderby’ work fine with your plugin. As soon as I use ‘posts_where’ the plugin and custom fields stop loading.

    To rule out that it is due to the creation of the ‘height’ taxonomy, I also tried other existing taxonomies and created more new taxonomies, either with the ‘Custom Post Type UI 1.13.4’ plugin or manually in the functions.php. The result is always the same, the ‘posts_where’ hook is triggering the problem.

    Do you have any idea what could be the cause of the problem? Do you have the possibility to reproduce the problem yourself using the Twenty Twenty data?

    Thank you very much for your help in advance!
    Andreas

    Plugin Author Abhishek

    (@abhisheksatre)

    Hi @andreas-2013,

    Thank you for providing the detailed steps.

    In order to further investigate the issue, could you please share the complete MySql query generated after the ‘posts_where’ hook is applied? You can use the query monitor plugin to check the generated query.

    Additionally, could you try using the get_custom_table_field() function instead of get_field to see if that resolves the issue?

    https://acf-custom-tables.abhisheksatre.com/docs/functions/get-custom-table-field/

    Thread Starter Andreas 2013

    (@andreas-2013)

    Hi Abhishek,

    thank you for your reply!

    I’ve tried ‘the_custom_table_field()’ in content.php, and it works! The custom fields are displayed now!

    In contrast to ‘get_field()’, is there any disadvantage with ‘the_custom_table_field()’, e.g. in terms of performance? Do you recommend the use of this function in principle?

    I tried to copy the SQL-queries from Query Monitor, but only got endless text without tabs. I don’t know, if this is useful?
    I compared the queries, with and without the hook. When not using ‘posts_where’, Query Monitor shows me queries like this (below ‘components’ = Plugin:acf-to-custom-database-tables):
    SELECT * FROM wp_tabletest WHERE post_id = 79518
    This Plugin-component is missing, when using ‘posts_where’.

    Plugin Author Abhishek

    (@abhisheksatre)

    Hi,

    the_custom_table_field function does not have any negative impact on performance. Please note that the function will not work if the plugin is deactivated.

    Thread Starter Andreas 2013

    (@andreas-2013)

    Ok, thank you! Do you already have an idea, why posts_where, get_field and your plugin don’t work together?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom fields aren’t loaded when using URL with parameter’ is closed to new replies.