SOLVED: Can't see the 404 template

I had a post about how to make certain page output conditions?

I never got a response, but I did it anyway.

add_action( 'template_redirect', 'my_404event', 1 );
function my_404event() {
    global $post, $wp_query;
    if ( is_singular( 'product' ) && get_post_status( $post )=='approved' && $post->post_author != get_current_user_id()) {
        $wp_query->set_404();
        status_header(404);
        include( get_query_template( '404' ) );
      //  include( get_404_template());
        exit;
    }
}

Displays the regular template 404 Bricks
And everything would be fine if not for one BUT … does not see the 404 template

And yet, you need to close the page from indexing, but to make it available, how to get the product template?

add_action( 'wp', function(){
    global $post;
    if ( is_singular( 'product' ) && get_post_status( $post )=='sold' ) {
        status_header( 503 );
         include(  get_query_template( 'single' )); <--------??????????
        exit;
    }

} );

Gives the page, but a blank screen

waiting for a solution!

Why is it necessary?
I need, I think, not only me, one thing is the condition for displaying content inside the page, another thing is the condition for displaying the page as a whole, how search engines see it.
For example, inside the template there are 3 sections with display conditions:

  1. The product is available for viewing, but without the buy button, since it is not yet in stock, everything is clear here. It is indexed, visible, returns 200.
  2. The goods are bought. Landing page returns 503 no matter what is indexed. Inside the template, the condition of another section works.
  3. The product is in custom status. The page gives 404, for those to whom it is not intended, but in fact it is, and the condition of another section works inside the template.

This applies not only to products, but also to other types.

@timmse
Good afternoon. Finally you are back.
Please pay attention to this post

Since I was directed to follow this thread by Itchycode on a page that had marked a 404 issue solved:

I’ll just update it with my scenarios where the custom 404 template doesn’t get used for some reason.

The code snippet below that uses

add_action( 'template_redirect', ...

will not use the Bricks 404 template but just shows the default one with the “Whoops, that page is gone” message.

// For example: https://domain.com/category/uncategorized/
// If not using Category Taxonomy deleting of Uncategorized Category not possible
function ess_disable_uncategorized_category_archives() {

    // if we are on Uncategorized category archive page
    if (is_category('uncategorized')) {
        global $wp_query;
        $wp_query->set_404();
        status_header(404);
    }

}

// Disable access
add_action( 'template_redirect', 'ess_disable_uncategorized_category_archives' );

I also have a code snippet similar to the above that 404’s home pages that the URL may contain unneeded pagination like:

mydomain.com/page/2/

The 404 status code is returned but neither the default 404 page or my custom 404 template is returned. Instead the homepage is returned (with a 404 status code) and anything that is queried is empty.

This issue also has been discussed in most of the other topics about 404 issues.

Hi @Tom ,

As the template mechanism in Bricks is more complicated, it wouldn’t be a quick fix from the dev team.

Hence, I would suggest you use Bricks filter to achieve what you want as a workaround for now.

Example: (change the 5135 to your error template ID)

add_filter( 'bricks/active_templates', 'itchy_disable_uncategorized_category_archives', 10, 3 );
function itchy_disable_uncategorized_category_archives( $active_templates, $post_id, $content_type) {
  if ( is_category( 'uncategorized' ) ) {
    global $wp_query;
    $wp_query->set_404();
    status_header( 404 );

    // Set the active templates to 404 template
    $active_templates['content'] = 5135;
    $active_templates['error'] = 5135;
    $active_templates['content_type'] = 'error';
  }
  return $active_templates;
}

Thanks that works and the custom 404 error template is used. It also works for a snippet I have for disabling pagination on custom post types and custom taxonomies (since I use a load more button on these and actually do not need separate pages).

It doesn’t catch the home page one though and instead a redirect happens back to the actual homepage.

mydomain.com/page/2/

This is better than redisplaying the homepage with empty queries as what happened before on non-existent pages though so that is okay.

still experiencing this issue:

example: https://floridaseating.com/product-category/quick-ship/outdoor-quick-ship/404asdf

yet it works here: https://floridaseating.com/sdcsdc

Hey,
For me, the 404 template also works with non-existent WooCommerce categories :thinking:

In order to provide a solution, I need to check the settings of your 404 error template. Could you please provide temporary admin access to your WordPress dashboard? Send the details to help@bricksbuilder.io, referencing this thread.

just sent. @timmse !

Hi @floridaseating ,

Thanks for the login details.
Identified that the FacetWP - Bricks Builder integration plugin causing the issue.
Deactivate the plugin and Bricks 404 template is working well.
Kindly reach out to their support. Thank you.

Regards,
Jenn

thanks for investigating. have contacted fwp regarding this issue. will update once resolved.

faucet wp devs sent a fix, and can report it has been corrected! assuming it will be merged into upcoming release.

:slight_smile:

@itchycode @timmse
By the way, the issue is not resolved.
Everything is still the same for me. Now there are no plugins, nothing can interfere, but it still gives different 404s, an example from the author of the post above:
1)
https://floridaseating.com/product-category/quick-ship/outdoor-quick-ship/404asdf

https://floridaseating.com/product-category23

how to deal with this?

Hi Maximus,
Would you be so kind as to send temporary login credentials and a link to this thread to help@bricksbuilder.io using the email address you used during the purchase? Then, one of the developers can look at your problem in detail.

it is resolved, just pushed the patch to production. it was not published before. but clicking both links results in expected behavior. at least for us.

1 Like

The fix we sent to @floridaseating will be included in the next update of our Bricks add-on.
Remco @FacetWP.com

1 Like