admin管理员组

文章数量:1167180

I am working on a site using Wordpress 6.0.2. Astra is the theme and I am using Elementor Pro.

I have lots of custom post types and taxomonies. I am using add_rewrite_rule like this:

add_rewrite_rule( 
    'thought-leadership/blog/([a-z0-9-]+)/([a-z0-9-]+)/page/([0-9]{1,})/?',
    'index.php?_post_type=blog&_$matches[1]_tax=$matches[2]&paged=$matches[3]',
    'top'
);
add_rewrite_rule( 
    'thought-leadership/blog/([a-z0-9-]+)/([a-z0-9-]+)/?',
    'index.php?_post_type=blog&_$matches[1]_tax=$matches[2]',
    'top'
);

An example URL: /thought-leadership/blog/service/quality-systems-and-compliance/

So the idea is it will list posts which have the post type of blog, and a taxonomy of service_tax which has the term slug quality-systems-and-compliance.

There are 5 different taxonomies that coule be used and hundreds of terms.

Pagination is set to 12 posts, but even when set to 6 I have the same error, and that is any page above 3 returns a 404 error.

I am using Query Monitor plugin to help me work out what is going on. This is what is showing on the Request tab:

Request
thought-leadership/blog/service/quality-systems-and-compliance/page/4

Matched Rule
thought-leadership/blog/([a-z0-9-]+)/([a-z0-9-]+)/page/([0-9]{1,})/?

Matched Query
_post_type=blog
&_service_tax=quality-systems-and-compliance
&paged=4

Query String
paged=4
&_post_type=blog
&_service_tax=quality-systems-and-compliance

I read a lot of posts and tried various solutions to no avail. For instance, I tried keeping the number of posts below the number of posts set in Settings > Reading. I have tried flushing permalinks.

To get this working in Elementor I have had to create an archive template which has the posts widget (this is where the number of posts is set) and then use this to map the template to this particular use:

function related_resource_archive_template($manager)
{
    if (ElementorPro\Modules\ThemeBuilder\Module::is_preview()) // Do not change content when editing.
        return;

    $theme_builder = ElementorPro\Modules\ThemeBuilder\Module::instance();
    $current_template = $theme_builder->get_conditions_manager()->get_documents_for_location('archive');
    
    $post_type = get_query_var( '_post_type' );
    
    if ( $post_type != '' && ( is_home() ) ) { // Replace `true` with your condition.
        $wanted_template = $theme_builder->get_document(3064); // Replace xxx with the ID of the template you want to display.
        $wanted_template->print_content(); // Display wanted template.
        $manager->set_is_printed('archive', key($current_template)); // Notify Manager that the archive location was rendered.
    }
}
add_action('elementor/theme/before_do_archive', 'related_resource_archive_template', 11);

In the posts widget I then use a Query ID which lets me set the tax query based on the taxonomy being used. This is the custom query:

function ele_query_resources_v2( $query ) {
    $post_type = get_query_var( '_post_type', 'blog' );
    $query->set( 'post_type', $post_type );
    
    $solution_tax = get_query_var( '_solution_tax' );
    $department_tax = get_query_var( '_department_tax' );
    $sector_tax = get_query_var( '_sector_tax' );
    $service_tax = get_query_var( '_service_tax' );
    $country_tax = get_query_var( '_country_tax' );
    
    $tax_query = $query->get( 'tax_query' ); 
    if( ! $tax_query ) {
        $tax_query = [];
    }
    if( $solution_tax != '' ) {
        $tax_query[] = [ 
            'taxonomy' => 'solution_tax', 
            'field' => 'slug',
            'terms' => $solution_tax, 
        ];
    }
    if( $department_tax != '' ) {
        $tax_query[] = [ 
            'taxonomy' => 'department_tax', 
            'field' => 'slug',
            'terms' => $department_tax, 
        ];
    }
    if( $sector_tax != '' ) {
        $tax_query[] = [ 
            'taxonomy' => 'sector_tax', 
            'field' => 'slug',
            'terms' => $sector_tax, 
        ];
    }
    if( $service_tax != '' ) {
        $tax_query[] = [ 
            'taxonomy' => 'service_tax', 
            'field' => 'slug',
            'terms' => $service_tax, 
        ];
    }
    if( $country_tax != '' ) {
        $tax_query[] = [ 
            'taxonomy' => 'country_tax', 
            'field' => 'slug',
            'terms' => $country_tax, 
        ];
    }
    $query->set( 'tax_query', $tax_query );
}
add_action( 'elementor/query/resources_v2', 'ele_query_resources_v2' );

This is also used to allow the query vars:

function themeslug_query_vars( $qvars ) {
    $qvars[] = '_post_type';
    $qvars[] = '_country_tax';
    $qvars[] = '_solution_tax';
    $qvars[] = '_sector_tax';
    $qvars[] = '_service_tax';
    $qvars[] = '_department_tax';
    return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' );

The confusing part is why it's always pages over 3 which have issues. I am not sure where to go next, so any ideas to help me debug this further would be great.

Steve

本文标签: custom taxonomyaddrewriterule pagination 404 error on page 4 and above