WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type list without a custom loop (10 posts)

  1. caugb
    Member
    Posted 3 years ago #

    Hi
    I'm working with a post type 'product', created in this way:

    $args = array(
        [...]
        'rewrite' => array('slug' => 'store', 'with_front' => false),
        [...]
      );

    in my local system I get the products list accessing http://mysite.com/store/ without any extra code, the products list happens naturally... but when I try it online, I get a 404 error (page not found).

    Searching this forum, I see several items showing custom loops to display custom posts. Is it necessary? So, why it works in my local server?

    The rewrite rules are the same in both installations (after flushing), but $wp_query shows that in local machine $wp_query->query_vars['post_type'] = 'product' while in online version, $wp_query points to a unknown 'page' - with exactly the same
    rewrite rules!!!

    PS. there is not a page or post with the slug 'store' .

    Any help would be appreciated

  2. molnarm
    Member
    Posted 3 years ago #

    Could you please post the whole register_post_type function call?

  3. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    If the function is more than 10 lines long, please use the pastebin.

  4. caugb
    Member
    Posted 3 years ago #

    Ok, here is the function that includes the post type: http://wordpress.pastebin.com/C0LCzdMz

  5. caugb
    Member
    Posted 3 years ago #

    Sorry, after paste I see that the code was with
    'rewrite' => array('slug' => 'loja', ...

    Just to say that this is not an error, ok? 'loja' is store in portuguese and I translate just for ask for your help.
    :)

  6. molnarm
    Member
    Posted 3 years ago #

    Try visiting the Permalink Settings page (online version) and save it (no need to actually change anything and ignore any messages related to modifying the .htaccess file) to recreate rules.

  7. caugb
    Member
    Posted 3 years ago #

    already did that... with no changes

  8. molnarm
    Member
    Posted 3 years ago #

    Custom post archives aren't created automatically (but coding them isn't difficult). So it seems the real question is "So, why it works in my local server?"

    It's worth noting that the upcoming 3.1 version will support custom post type archives.

  9. caugb
    Member
    Posted 3 years ago #

    Thank you, molnarm. The hope is dead, but now I kown what to do!

  10. caugb
    Member
    Posted 3 years ago #

    I know there are many topica here about it, but I solved the problem with this simple function:

    // adapted from Simple Custom Post Type Archives
    function mss_rewrite_rules($wp_rewrite) {
      $post_type = get_post_type_object('product');
      $type_slug = $post_type->rewrite['slug'];
      $new_rules = array(
        $type_slug.'/?$' => 'index.php?post_type=product',
        $type_slug.'/page/?([0-9]{1,})/?$' => 'index.php?post_type=product&paged='.$wp_rewrite->preg_index(1),
        $type_slug.'/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=product&feed='.$wp_rewrite->preg_index(1),
        $type_slug.'/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=product&feed='.$wp_rewrite->preg_index(1)
      );
      $wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules);
    }
    add_action('generate_rewrite_rules', 'mss_rewrite_rules' );

    But why it works in my local server (without this function) is still a mistery.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.