Random 404 on Custom Post Types

Hi,

So far my frontity experience has been great with some struggles, but it takes some time to learn everything :slight_smile:

I am getting very frustrated because i am getting random 404 on some custom post type pages.

All data is getting fetched on the archive pages, but sometimes I’m not able to click into the single review.


Here’s a copy of my settings-file.

    const settings = {
  "name": "kreditdebit-frontity",
  "state": {
    "frontity": {
      "url": "https://kreditdebit.com",
      "title": "KreditDebit",
      "description": "Jämför lån för dina behov online"
    }
  },
 "packages": [
    {
      "name": "@frontity/google-analytics",
      "state": {
        "googleAnalytics": {
          "trackingId": "G-KTDBJQ3SQ3",
        },
      },
    },
  ],
  "packages": [
    {
      "name": "@awsmin/f1",
      "state": {
        "theme": {
          "menu": [
            [
              "Start",
              "/"
            ],
            [
              "Privatlån",
              "/privatlan"
            ],
            [
              "Finansinsitut",
              "/finansinsitut/"
            ],
            [
              "Låna pengar",
              "/lana-pengar/"
            ],
            [
              "Om oss",
              "/om-oss/"
            ]
          ],
          "featured": {
            "showOnList": true,
            "showOnPost": true
          }
        }
      }
    },
    {
      "name": "@frontity/wp-source",
      "state": {
        "source": {
          "api": "https://wp.kreditdebit.com/wp-json",
          "homepage": "/homepage",
          "postsPage": "/artiklar",
          postTypes: [
            {
              type: "prp_reviews", // type slug
              endpoint: "prp_reviews", // REST API endpoint
              archive: "/finansinsitut" // link where this custom posts are listed
            }
          ]
        }
      }
    },
    "@frontity/tiny-router",
    "@frontity/html2react",
    "@frontity/yoast",
    "frontity-contact-form-7"        
    ]   
};

export default settings;

Hi @marten

Can you provide a link to a repo so that we can look at your code and clone it to see what it’s doing.

Also if you could give me some guidance on where to look and what to click on the site to find the CPT and reproduce the problem that would be helpful, as I don’t understand the language.

Thanks.

Ah, ok - I think I found the problem. The post types are listed on the Finansinsitut page. As far as I can see all the single pages load, but if you refresh the page when you’re on a single page, i.e. make it your landing page, then you get a 404.

Please let me know if that is the correct diagnosis. Thanks.

Yes, that is one of the problems I am experiencing. Also sometimes when coming from the archive (finansinstitut) and clicking into a single might cause a 404.

Am I loading the cpt correctly?

I haven’t experienced a 404 by clicking into a single CPT page, only when refreshing when on a single CPT page. Clicking directly seems to be working consistently correctly on my end.

Your settings.js looks correct, and probably is because it appears to be working, but it’s hard to say for sure without knowing how your CPT is set up in WordPress, i.e. what arguments the register_post_type function is receiving.

Here’s the cpt registeration

public function poka_review_register_cpt(){

    //The Review Custom Post Type
    $labels = array(
        'name' => _x('Reviews', 'post type general name', 'poka-review'),
        'add_new' => _x('Add new review', 'member', 'poka-review'),
        'add_new_item' => __('Add new review', 'poka-review'),
        'edit_item' => __('Edit', 'poka-review'),
        'new_item' => __('New review', 'poka-review'),
        'view_item' => __('View', 'poka-review'),
        'search_items' => __('Search reviews', 'poka-review'),
        'not_found' =>  __('No reviews found!', 'poka-review'),
        'not_found_in_trash' => __('No reviews in the trash!', 'poka-review'),
        'parent_item_colon' => ''
    );

    $args = array(
      'labels' => $labels,
      'public' => false,
      'publicly_queryable' => false,
      'show_ui' => true,
      'capability_type' => 'post',
      'show_in_nav_menus' => false,
      'show_in_menu' => true,
      'menu_icon' => 'dashicons-archive',
      'hierarchical' => false,
      'rewrite' => array( 'slug' => 'prp_reviews', 'with_front' => false ),
      'supports' => array('title'),
      'has_archive' => false
    );

Its from a plugin, so i have added a hook into my function.php file

add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
 
function my_post_type_args( $args, $post_type ) {
 
    if ( 'prp_reviews' === $post_type ) {
        $args['show_in_rest'] = true;
     }
 
    return $args;
}

That all looks fine TBH. You could experiment a little and try changing the rewrite.with_front property to true, or changing the publicly_queryable and/or public properties to true to see if that makes any difference. It’s worth trying but I’m not sure that it will make a difference to what appears in the REST API, though changing the rewrite.with_front property might make a difference.

A link to your repo so we can look at/clone the code would be useful to help you further.

Another quick question, because you didn’t include it in the CPT registration code snippet above - is the actual definition like this?:

register_post_type('prp_reviews', $args);

I ask because I’m wondering why they’re rewrite-ing the slug item.

Yes, the cpt registers with

    register_post_type('prp_reviews',$args);

Here’s a link to the repo: https://github.com/moffepoffe/kreditdebit

Hi @marten

I’ve cloned your repo and as before I consistently get a single page when I click a link from the Finansinsitut page. I only get an error when I refresh a single CPT page - this also occurs consistently.

I’ve had a quick look at your code, but there’s a lot going on there and I didn’t spot anything obviously untoward. I suspect that due to the consistent behaviour you’ve got something that works in the browser but not on the server during SSR. For example, in the <FixedHeader> component you’re calling document.addEventListener. I don’t think this is the actual problem, but there might be something else which is causing the problem. It might be something that you need to add in beforeSSR action.

Thanks for taking you time to help me. I really appreciate it! Thanks for the input on the addeventlisterner event. It’s on my todo list to solve.

I will update this thread when i find the solution to the 404 problem.

@marten could you please create a Codesandbox with your project so we can take a look?

It’s as easy as adding a sandbox.config.json file with this in the root of your project:

{
  "template": "node"
}

and then the Codesandbox will start when you visit: https://githubbox.com/moffepoffe/kreditdebit

More info about Codesandbox here: Add CodeSandbox config by default when creating Frontity projects with `npx frontity create`

Once you’ve done it please let me know and we’ll take a look to see if we can find out more about those 404 errors.

Sorry for the late reply! Im trying to get the sandbox up and running, but there seems to be an issue. Getting som API rate exceeded error. I will try in an hour or so.

Is there any faster way to communicate, like slack or something? :slight_smile:

This forum is the best place. You can turn on the notifications if you want :slightly_smiling_face: