• Resolved shoxt3r

    (@shoxt3r)


    Hi there,

    I’m currently using the plugin WP Store Locator and am having problems getting the “All Stores” page to use a template. I’ve got the Custom Post Type of “theatre” setup and it’s displaying the posts I need.

    I’ve gone down the usual route of registering the Custom Post Type using the following settings:

        // Theatre Post Type
        register_post_type('theatre', array(
            'show_in_rest' => true,
            'supports' => array('title', 'excerpt'),
            //'rewrite' => array('slug' => 'theatres'),
            'has_archive' => true,
            'public' => true,
            'labels' => array(
                'name' => 'Theatres',
                'add_new_item' => "Add New Theatre",
                'edit_item' => "Edit Theatre",
                'all_items' => "View All Theatres",
                'singular_name' => 'Theatre'
            ),
            'menu_icon' => 'dashicons-location-alt'
        ));

    I’ve created the pages “archive-theatre.php”, “page-theatre.php” and “single-theatre.php” (at the moment “page-theatre.php” is probably redundant but I’m trying everything at the moment to get the template to pull through). “single-theatre.php” is working fine and pulling through the individual “theatre” post type details and is linked to from the parent “theatre” page. However, the “theatre” page itself is defaulting to using the “index.php” as a template which is not what I want.

    I’ve saved the “Permalinks” within the “Settings” to see if that’s the problem but that doesn’t seem to affect anything.

    Any ideas what the problem may be and how I can diagnose the bug?

    I have setup a “films” Custom Post Type which works fine (it uses “archive-film.php” and “single-film.php” as its page templates) but doesn’t use the WP Store Locator plugin which I suspect may be causing a conflict but I can’t be certain.

    Here is the Permalink setting I currently have set within the WP Store Locator settings:
    https://pasteboard.co/Kf0hLTO.png

    Also, I’ve created a post on the WP Store Locator plugin forum but it seems this issue requires a custom rewrite rule which neither myself or the plugin developer know how to do. Here is the post I created on their forums:
    https://ww.wp.xz.cn/support/topic/acf-custom-post-type-with-store-locator/

    If any other details would be helpful please let me know.

    Many thanks!

    • This topic was modified 4 years, 10 months ago by shoxt3r.
    • This topic was modified 4 years, 10 months ago by shoxt3r.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter shoxt3r

    (@shoxt3r)

    Ah ok – I just tried disabling the plugin and the “theatre” page now uses the correct template of “archive-theatre.php” but it also reverts the “Store Locator” menu option within the Admin area back to “Theatres” which matches the settings I posted earlier.

    As soon as I activate the plugin again, it changes the menu option in the admin to “Store Locator” and reverts back to using the “index.php” page for the template.

    I suspect therefore there is a conflict or something I’ve missed when changing the “stores” references to “theatres” – any thoughts please?

    My only option may be to start again with the plugin and keep it as “stores” for now and see how I get on.

    Moderator bcworkz

    (@bcworkz)

    I suspect the plugin is forcing a specific template through the “template_include” filter. It’s a common technique, but I’m only speculating as it relates to that specific plugin. If this is indeed what the plugin does, you can override the plugin’s template choice with your own by hooking the same filter, but adding with a larger priority number so your callback’s template selection has the final say.

    Almost all requests go through this filter. Be sure you only alter the template of the specific page you wish to target. The returned value is immediately included by WP, so you should return a complete path to the desired template.

    Your callback isn’t given much context, but the various is_*() functions like is_single() will work. You can examine the query vars used through the global $wp_query object properties. If you are targeting a single page, you can get its ID through get_queried_object_id().

    Thread Starter shoxt3r

    (@shoxt3r)

    I had a search for “template_include” but it isn’t listed in any of the plugin’s files.

    After searching for the term “template” I did come across the following code, however, but I believe this only refers to the following documentation – I’m unsure if it’s even relevant but will write back to the plugin creator and find out.

          public function show_template_options() {
                
                global $wpsl_settings;
                
    			$dropdown = '<select id="wpsl-store-template" name="wpsl_ux[template_id]" autocomplete="off">';
    
                foreach ( wpsl_get_templates() as $template ) {
                    $template_id = ( isset( $template['id'] ) ) ? $template['id'] : '';
                    
    				$selected = ( $wpsl_settings['template_id'] == $template_id ) ? ' selected="selected"' : '';
    				$dropdown .= "<option value='" . esc_attr( $template_id ) . "' $selected>" . esc_html( $template['name'] ) . "</option>";
                }
    			
    			$dropdown .= '</select>';
    			
    			return $dropdown;            
            }

    https://wpstorelocator.co/document/load-custom-store-locator-template/

    Any thoughts based on that?

    • This reply was modified 4 years, 10 months ago by shoxt3r.
    • This reply was modified 4 years, 10 months ago by shoxt3r.
    Thread Starter shoxt3r

    (@shoxt3r)

    Ok I think the above was a red herring since I wrote back to the plugin developer and he came back with the following:

    There’s this code in the class-frontend.php that creates the default front-end template. But there’s nothing else in the code that forces a specific template.

    But it shouldn’t run if you created a single-your-cpt.php.

    I’ve now gone back through a fresh version of the plugin and replaced anything related to “store” or “stores” with “cinema” or “cinemas”. However, while going through the “class-post-types.php” file I spotted it registers a post type using the following code:

                // The arguments for the cinema post type.
                $args = apply_filters( 'wpsl_post_type_args', array(
                        'labels'              => $labels, 
                        'public'              => $public,
                        'exclude_from_search' => $exclude_from_search,
                        'show_ui'             => true,
                        'menu_position'       => apply_filters( 'wpsl_post_type_menu_position', null ),
                        'capability_type'     => 'store',
                        'map_meta_cap'        => true,
                        'rewrite'             => $rewrite,
                        'query_var'           => 'cinema',
                        'supports'            => array( 'title', 'editor', 'author', 'excerpt', 'revisions', 'thumbnail' ),
                        'show_in_rest'        => $this->maybe_show_in_rest()
                    )
                );
    
                register_post_type( 'cinema', $args );

    Note that here, it’s registering my new post type of “cinema”.

    Now, I don’t know whether the rewrite is coming from the WP Store Locator Settings, or whether the following code I have in my “apex-post-types.php” file, but the “cinema” slug now redirects to “cinemas” which is what I want (I would guess my code in “apex-post-types.php” is redundant to be honest).

     function wp1482371_custom_post_type_args( $args, $post_type ) {
                if ( $post_type == "cinema" ) {
                    $args['rewrite'] = array(
                        'slug' => 'cinemas'
                    );
                }
            
                return $args;
            }
            add_filter( 'register_post_type_args', 'wp1482371_custom_post_type_args', 20, 2 );

    Now I’m in a situation whereby the “single” pages are working, but I still can’t get an archive or regular page to show, despite calling it “archive-cinema.php” and “page-cinema.php” to match the new post type registered via “class-post-types.php”.

    Any thoughts why the main page may not be loading? I just get a 404 when I try to visit /cinemas (which is rewritten from /cinema, remember :))

    Would it help to do a site export so you can see for yourself what the issue may be?
    I’ve done several Permalink saves and nothing seems to resolve it.

    • This reply was modified 4 years, 10 months ago by shoxt3r.
    Moderator bcworkz

    (@bcworkz)

    The 404 page is a WP styled “Nothing Found” sort of page and not a server (usually unstyled) File not Found sort of 404, correct? It’s not unheard of for the SQL query that was used to be incorrectly constructed such that nothing could possibly be found. By examining the actual SQL, the part of the query that is erroneous is often a strong indication of the root cause, or at least a clue towards development of a reasonable workaround.

    For example, perhaps the query is for post type “cinemas” instead of the correct “cinema”. Altering the post_type query var through the “pre_get_posts” action when it is incorrectly set to “cinemas” would cure the problem. I’m not saying this is the cause here, it’s just an example.

    To see the SQL query actually used, use the Query Monitor plugin. After viewing all queries (via submenu item below the statistics showing in the admin bar), filter the caller by “Main Query” to see the query that should have returned an archive of cinema posts.

    Thread Starter shoxt3r

    (@shoxt3r)

    Hi there,

    The error is appearing on a custom “404.php” page I created, so it’s just a text-based message for now but isn’t a “server error” if that helps?

    Thanks for recommending the Query Monitor plugin – I’ve loaded it up and navigated to the “Cinema” Post Type page and selected “Main Query” from the “Caller” dropdown and this is what I see:

    https://pasteboard.co/KfrPuwn.png

    One thing I have noticed is that if I change the page name of “single-cinema.php” to “single-cinemas.php”, then the individual cinema page breaks – but still neither my “archive-cinema.php” and “page-cinema.php” pages work, they just 404. I was hoping it was something as simple as that before I posted as I’ve done that mistake before but no such luck.

    Would it help if I posted a link to download the project?

    Moderator bcworkz

    (@bcworkz)

    If there’s a query monitor result, then it cannot be a server based 404, it’s within WP. Unfortunately, there’s nothing about that query that should cause a 404 unless there were no cinema posts at all.

    I’m willing to take a quick look at your project, but I cannot promise that I’ll go so far as to install it and do a deeper investigation. In a quick look, maybe something will jump out, or maybe I’ll be intrigued enough to dig deeper. ¯\_(ツ)_/¯

    Thread Starter shoxt3r

    (@shoxt3r)

    Thanks for the feedback.
    However – I’ve found the issue in the meantime. Just to feedback for anyone else coming across this thread – turns out I made the rookie error of not adding “has_archive” => true to the arguments list when the post type is registered. Oops!

    Moderator bcworkz

    (@bcworkz)

    It seems a bit strange to default to false, but that’s how it is. I’m glad you found the problem!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Custom Post Type with WP Store Locator plugin’ is closed to new replies.