• Resolved charlie67p

    (@charlie67p)


    Hi

    I just updated from 1.0.10 to 1.1.0

    On my CPT archive page, the meta field block is not displaying the values anymore.

    Using this in my template archive-cpt.html :
    <!– wp:mfb/meta-field-block {“fieldType”:”acf”,”fieldName”:”year_value”} /–>

    So I downgraded to 1.0.10 (where it works fine ! )

    Thanks for checking that

    • This topic was modified 1 year, 8 months ago by charlie67p.
Viewing 5 replies - 16 through 20 (of 20 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hi?@charlie67p

    I found the root cause of the issue.

    The main reason is a snippet code in your child theme that alters all custom queries on this template, here is your code:

    function my_pre_get_posts( $query )
    {
        // do not modify queries in the admin
        if(is_admin() ) {
            return $query;
        }
    
        if (( $query->is_tax() ) OR (is_post_type_archive('projet')) ) {
            $query->set('orderby', 'meta_value');
            $query->set('meta_key', 'your_field_name');
            $query->set('order', 'DESC');
        }
        if (is_front_page() ) {
            $query->set('cat', '-14');
        }
        // return
        return $query;
    }
    add_action('pre_get_posts', 'my_pre_get_posts');

    There is no check in this hook to determine whether the query object is the main query. This will affect all custom queries. You can test it by dropping a core dynamic block like latest-posts on this template, you will see that no items are loaded.

    Since MFB version 1.1.x, I’ve added more features under the hood to support more complex fields in future versions, that’s why it fails to display the meta value. 

    The solution: 

    In my part, I’ll add some code in the next version of the plugin to cover these rare use cases, so it sill can display the value for basic fields like version 1.0.x.

    For your child theme, I suggest you should add a simple check in your hook to solve all potential issues related to this query stuff, here is the code:

    function my_pre_get_posts( $query )
    {
        // do not modify queries in the admin
        if(is_admin()) {
            return $query;
        }
    
        // Only modify the main query
        if ( ! $query->is_main_query() ) {
          return $query;
        }
    
        if (( $query->is_tax() ) OR (is_post_type_archive('projet')) ) {
            $query->set('orderby', 'meta_value');
            $query->set('meta_key', 'your_field_name');
            $query->set('order', 'DESC');
        }
        if (is_front_page() ) {
            $query->set('cat', '-14');
        }
        // return
        return $query;
    }
    add_action('pre_get_posts', 'my_pre_get_posts');

    After adding the check, the latest version of the block will work fine for your fields, you can test it yourself.

    Thanks, Phi.

    • This reply was modified 1 year, 8 months ago by Phi Phan.
    Thread Starter charlie67p

    (@charlie67p)

    Hi @mr2p, Thanks for checking,
    OK but then my orderby meta_value is not working anymore…

    Plugin Author Phi Phan

    (@mr2p)

    @charlie67p Is the query loop a main query or not? The main query is the one with the inherit checkbox enabled on the Query Loop block.

    In my previous reply, I’m supposed the query is the main query. If it’s not you need to replace that check with a custom check for your custom query.

    Thread Starter charlie67p

    (@charlie67p)

    Ah yes! you’re right
    My wp:query “inherit” was set to false. After setting it to true it works fine ??

    Thank you again @mr2p

    Plugin Author Phi Phan

    (@mr2p)

    I’m glad it works for you now @charlie67p

    Next time if you want to alter a query loop block, I think this is the right way to do it https://developer.www.ads-software.com/reference/hooks/query_loop_block_query_vars/

    Phi.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘version 1.1.0 not working anymore on CPT archive page’ is closed to new replies.