0

I have custom table "catalog" inside wordpress database:

id|name|description|slug|price

I have template file "catalog" and page "Catalog" where i show all items from catalog table. But how i can show single item from this list as a page?

domain.dev/catalog/ (this part works) domain.dev/catalog/my-single-item-page (this i can't figure out how to make to work)

how i can use "my-single-item-page " as dynamic variable?

slug = "my-single-item-page";

i have this snippet inside my functions.php file

    function add_query_vars_catalog_item_filter( $vars ) {
        $vars[] = "catalog_item";
        return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_catalog_item_filter' );

    function catalog_rewrite() {
        add_rewrite_rule('^catalog/([\w-]+)/?', 'index.php?pagename=catalog&catalog_item=$matches[1]', 'top');
    }
    add_action('init', 'catalog_rewrite');

but when i go to domain.dev/catalog/some-my-item i got 404 page

I use this custom table because i generate form CSV. And in future it will be simpler for regenerate data

  • 1
    Have you flushed the rewrite rules? (just visit the permalink settings page) – Sally CJ Aug 16 at 9:10
  • 1
    @SallyCJ Are you serious? Such a quick thing. How i do not think about that :D. Thank you!!! You can write answer and i will accept :) – napalias Aug 16 at 18:35
1

Your code worked absolutely fine for me, so I suspected you forgot to flush the rewrite rules.

Each time after you added code which alters the WordPress rewrite rules, e.g. add_rewrite_rule() or code which registers/un-registers a CPT or custom taxonomy with URL rewriting enabled, you should always flush the rewrite rules which means WordPress will empty/reset the saved rewrite rules (in the database and .htaccess file) and regenerate (and re-save) them which would include your newly added or updated rewrite rules.

How to flush the rewrite rules: Just visit the Settings → Permalink Settings page. And there's no need to click on the "Save Changes" button.

There's also a programmatic way (see flush_rewrite_rules()), but in general, the above trick is the preferred way unless you are writing a plugin where you would for example, call the function during the plugin activation and deactivation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.