phpwordpressfunctionwoocommerceshortcode

Can't display the [products] shortcode on a page


I'm trying to display the shortcode [products] on a new page I created. The products don't display, (but the shortcode itself doesn't show as text (as it would if it didn't render, meaning the shortcode functions but doesn't display anything). I checked the logs and it gives these errors:

Function is_post_type_archive was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false.

Function is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false.

Function is_singular was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false.

Function is_post_type_archive was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false.

The errors happens in /home/miiriya/public_html/wp-includes/functions.php on line 6121

Is there a workaround I can add to my functions.php to allow for the products to be added to a page?

Let's say the page where I'm trying to display it is ex: https://www.example.com/fashion-beauty/

I saw something similar here, but I don't know how to implement it:

https://wordpress.stackexchange.com/questions/242910/is-search-was-called-incorrectly

I apologize for the trouble.


Solution

  • You need to delay your logic until the query is available, such as using the wp hook or inside template files with proper checks.

    If you're trying to modify how the [products] shortcode behaves, or fix custom code depending on is_page() / is_post_type_archive() etc., wrap your code in the correct hook like this:

    add_action( 'wp', 'my_custom_woocommerce_logic' );
    
    function my_custom_woocommerce_logic() {
        // Now it's safe to use conditional tags
        if ( is_page( 'fashion-beauty' ) ) {
            // Your custom logic here
        }
    }