错误的CPT和税费请求查询

时间:2014-12-21 作者:Kresimir Pendic

在我的插件中,我想我使用了很多标准和文档化的方法来添加我的自定义帖子类型和两个税(猫和标签),但我唯一要做的就是设置自定义capability_type 还有烫发。

以下是代码:

// =============================
// custom taxonomies (cats and tags)
// =============================
function custom_taxonomy() {

    $labels = array(
        \'name\'                       => _x( \'ifcc Categories\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'ifcc Category\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'ifcc category\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Items\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'text_domain\' ),
        \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'rewrite\'                    => array( \'slug\' => \'ifcc-categories\' ),
        \'query_var\'                  => true
    );
    register_taxonomy( \'ifcc_cats\', array( \'ifcc\' ), $args );

    $labels = array(
        \'name\'                       => _x( \'Tags\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Tag\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Tag\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Items\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'text_domain\' ),
        \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'query_var\'                  => true
    );
    register_taxonomy( \'ifcc_tags\', array( \'ifcc\' ), $args );

}
add_action( \'init\', \'custom_taxonomy\', 20 );

// =============================
// register ifcc custom post type for new tax up
// =============================
function custom_post_type() {
    global $woo_contest;

    $labels = array(
        \'name\'                => _x( \'IFCC Submits\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'IFCC Submit\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'IFCC Submit\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Items\', \'text_domain\' ),
        \'view_item\'           => __( \'View Item\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Item\', \'text_domain\' ),
        \'add_new\'             => __( \'Add New\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Item\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Item\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Item\', \'text_domain\' ),
        \'not_found\'           => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' ),
    );
    $args = array(
        \'label\'               => __( \'post_type\', \'text_domain\' ),
        \'description\'         => __( \'ifcc submit\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'revisions\', \'custom-fields\', \'post-formats\', \'author\', \'thumbnail\' ),
        \'taxonomies\'          => array( \'ifcc_cats\', \'ifcc_tags\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 20,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => true,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'ifcc\',
    \'capabilities\'        => array( \'read_ifcc\', \'edit_ifcc\', \'delete_ifcc\' ),
    \'map_meta_cap\'        => true
    );
    register_post_type( \'ifcc\', $args );

    register_taxonomy_for_object_type( \'ifcc_cats\', \'ifcc\' );
    register_taxonomy_for_object_type( \'ifcc_tags\', \'ifcc\' );
    // flush_rewrite_rules();

}

add_action( \'init\', \'custom_post_type\', 30 );
然后我添加它来修复管理员和客户(该cpt的真实用户,但仅用于前端任务,上载图片并将表单提交到后端,保存帖子)

// =============================
// fix admin and woo user perms
// =============================
function ifccperms(){
    $role = get_role(\'administrator\');
    $role->add_cap( \'edit_ifcc\' );
    $role->add_cap( \'read_ifcc\' );
    $role->add_cap( \'delete_ifcc\' );
    $role->add_cap( \'delete_ifccs\' );
    $role->add_cap( \'edit_ifccs\' );
    $role->add_cap( \'edit_others_ifccs\' );
    $role->add_cap( \'publish_ifccs\' );
    $role->add_cap( \'read_private_ifccs\' );
    $role->add_cap( \'delete_private_ifccs\' );
    $role->add_cap( \'delete_published_ifccs\' );
    $role->add_cap( \'delete_others_ifccs\' );
    $role->add_cap( \'edit_private_ifccs\' );
    $role->add_cap( \'edit_published_ifccs\' );    

    if( null == add_role( \'customer\', \'customer\' ) ){
        $role = get_role(\'customer\');
        $role->add_cap( \'edit_ifcc\' );
        $role->add_cap( \'edit_ifccs\' );
        $role->add_cap( \'delete_ifcc\' );
        $role->add_cap( \'delete_ifccs\' );
        $role->add_cap( \'edit_published_ifccs\' );    
        $role->add_cap( \'publish_ifccs\' );
        $role->add_cap( \'read_ifcc\' );
        $role->add_cap( \'upload_files\' );            
    }

}
add_action( \'init\', \'ifccperms\', 40 );



// =============================
// dirty hack to alter sql query == do not know why does the query works wrongly?
// here I just alter one
// =============================

function checkq($a){
    return str_replace(\'product\', \'ifcc\', $a );
}
add_filter( \'posts_request\', \'checkq\' );
一切都很好,在后端我可以看到cpt和tax,每个用户只能看到自己的帖子,从前端上传效果很好,等等。

但问题来了。当我尝试从前端访问分类页面时

root/ifcc-categories/
页面转到“未找到页面”,404错误。

我确实钩住检查了保存页面的sql,它回应了我:

SELECT wp_posts.* 
FROM wp_posts 
WHERE 1=1 
AND wp_posts.post_name = \'ifcc-categories\' 
AND wp_posts.post_type = \'post\' 
ORDER BY wp_posts.post_date DESC
我对两件事感到困惑。为什么wordpress会这么认为post_name 岗位类型为岗位?

还有一件事,如果我转到术语url,例如:

root/ifcc-categories/design/
我得到以下sql字符串:

SELECT ...  WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (7) ) 
AND wp_posts.post_type 
IN (\'post\', \'page\', \'attachment\', \'product\') 
AND (wp_posts.post_status = \'publish\' 
OR wp_posts.post_author = 1 
AND wp_posts.post_status = \'private\')
如果有人对这里发生的事情有任何线索,我很高兴听到,因为我迷路了

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

这个问题的第一部分以前已经回答过了。基本上,档案没有索引页,将来也不会有。有关完整的解释,请随时查看this post 我最近在这个问题上做了很多工作。

为什么wordpress会这么认为post_name 岗位类型为post?

你可能知道也可能不知道,但Wordpress使用WP_Query 要运行主查询,唯一的区别是WP_Query 是指对于新实例,参数值由用户设置,而主查询使用URL来设置这些参数和值

好吧,那么这里到底发生了什么?当单击任何链接或在地址栏中键入任何URL时,Wordpress将执行主查询。传递给的参数和值WP_Query 由URL确定。Wordpress解析URL,匹配重写规则,并将URL分解为作为查询参数传递的值。

如果你有/%postname%/ 设置为永久链接结构,帖子的URL如下所示

我的网站。com/我的帖子名称/

因此,如果您直接在URL中输入分类名称,则如下所示

我的网站。com/ifcc类别/

如您所见,这两个URL的结构完全相同,Wordpress就是这样读取的。您必须记住,主查询使用重写规则来匹配URL,在本例中,它匹配普通帖子。这就是您在问题中看到SQL查询的原因

主查询默认为post 所有页面上的帖子类型在自定义帖子类型存档页面、普通页面上接受(在这种情况下,帖子类型将为page) 和分类页面。

因为你没有一篇文章(或页面)有这个名字ifcc-categories, 返回404。如果找不到任何内容,主查询永远不会停止,即使将无效值设置为以参数开头(在任何情况下,主查询都不会知道),它只会继续执行并完成它开始的内容,即使在404页上也是如此。这就像丢了一个球。那个球不能在空中停下来,因为没有人接住它。它只有在落下后才能停下来

最后,如果需要为特定的术语、类别或分类法创建索引页,则需要创建一个包含所需自定义查询的页面,并相应地命名slug,以便在访问

我的网站。com/ifcc类别

您将获得您创建的索引页

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post