如何根据 Wordpress 中的自定义分类法过滤存档帖子
How to filter archive posts based on custom taxonomy in Wordpress
我创建了一个带有自定义分类的自定义 post 类型。为此,我在 functions.php
中使用了以下代码
//Custom Posttype
function custom_post_type_rental() {
// source: https://wordpress.stackexchange.com/questions/156978/custom-post-type-single-page-returns-404-error
$labels = array(
'name' => _x( 'Rentals', 'i2sa'),
'singular_name' => _x( 'rentals', 'i2sa'),
'menu_name' => _x( 'Rentals', 'i2sa'),
'name_admin_bar' => _x( 'Admin Bar', 'i2sa'),
'add_new' => _x( 'Nieuwe rental', 'i2sa'),
'add_new_item' => __( 'Voeg nieuwe rental toe', 'i2sa'),
'new_item' => __( 'Nieuw item', 'i2sa'),
'edit_item' => __( 'Bewerk rentals', 'i2sa'),
'view_item' => __( 'Bekijk rentals', 'i2sa'),
'all_items' => __( 'Alle rentals', 'i2sa'),
'search_items' => __( 'Zoek rentals', 'i2sa'),
'parent_item_colon' => __( 'Parent rentals:', 'i2sa'),
'not_found' => __( 'Zoekopdracht niet gevonden.', 'i2sa'),
'not_found_in_trash' => __( 'Zoekopdracht niet gevonden in prullenbak.', 'i2sa')
);
$args = array(
'labels' => $labels,
'description' => "Beschrijving i2sa",
'public' => true, //openbaar
'exclude_from_search'=> true, //uitschakelen voor zoekopdrachten
'publicly_queryable' => true, //publiekelijk vindbaar
'show_in_nav_menus' => false, //toon in navigatie
'menu_icon' => 'dashicons-bank',
'show_ui' => true,
'show_in_rest' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'rental' ), //custom url
'capability_type' => 'post',
'has_archive' => true, //heeft archivepage
'hierarchical' => false, // true maakt het een pagina
'menu_position' => null, //null = geen
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'author', 'trackbacks', 'comments', 'revisions', 'post-formats', 'page-attributes' ), //gesupporte functionaliteiten
//je kan de support 'thumbnail' ook vervangen door ‘attachment:audio’ and ‘attachment:video’
);
register_post_type( 'rental', $args );
//flush_rewrite_rules();
}
add_action( 'init', 'custom_post_type_rental' );
function rentalMachineSoort() {
//Labels voor de custom taxonomy
$labels = array(
'name' => _x( 'Machinesoorten', 'machinesoorten' ),
'singular_name' => _x( 'Machinesoort', 'machinesoort' ),
'search_items' => __( 'Search Machinesoorten' ),
'all_items' => __( 'All Machinesoorten' ),
'parent_item' => __( 'Parent Machinesoort' ),
'parent_item_colon' => __( 'Parent Machinesoort:' ),
'edit_item' => __( 'Edit Machinesoort' ),
'update_item' => __( 'Update Machinesoort' ),
'add_new_item' => __( 'Add New Machinesoort' ),
'new_item_name' => __( 'New Machinesoort Name' ),
'menu_name' => __( 'Machinesoorten' ),
);
// Taxonomy registreren
register_taxonomy('machinesoort',array('rental'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'machinesoort' ),
));
}
add_action( 'init', 'rentalMachineSoort', 0 );
在我的存档中,我想按此分类法过滤 post。为此,我使用了代码:
<form method="GET">
<select name="orderby" id="orderby">
<?php
$terms = get_terms([
'taxonomy' => 'machinesoort',
'hide_empty' => 'false'
]);
foreach ($terms as $term) :
?>
<option value="<?php echo $term->slug;?>" name="machinesoort[]"><?php echo $term->name;?></option>
<?php endforeach;?>
</select>
<button type="submit">Filter</button>
这会使页面刷新为包含参数的新 url。问题是页面仍然显示所有文章,包括不应该显示的文章。
任何人都知道我怎样才能让这段代码正常工作以便它能正确过滤?如果我只使用 checkboxxes 它工作正常,但我需要使用这个过滤器中的方法
"Problem is that the page still shows all articles, including the articles that should not be shown."
因为你错过了主要步骤!您没有根据所选值更改查询!
您可以使用 pre_get_posts
挂钩来完成。像这样:
add_action('pre_get_posts', 'altering_rental_archive_query', 99);
function altering_rental_archive_query($query)
{
if (
is_post_type_archive('rental')
&&
get_query_var('orderby')
)
{
$tax_query = array(
array(
'taxonomy' => 'machinesoort',
'field' => 'slug',
'terms' => sanitize_text_field(get_query_var('orderby')),
)
);
$query->set('tax_query', $tax_query);
};
};
结果如下:
在没有任何过滤器/查询变量的情况下首次加载存档页面时:
如果我按第一个自定义分类法过滤它:
如果我按第二个自定义分类法过滤它:
快一点 tip/bonus:
您可以将 selected
Docs 功能添加到 archive
页面上的表单,以便根据 url 上查询的变量正确选择过滤器下拉列表:
<form method='GET'>
<select name='orderby' id='orderby'>
<?php
$terms = get_terms([
'taxonomy' => 'machinesoort',
'hide_empty' => 'false'
]);
foreach ($terms as $term) :
?>
<option value='<?php echo $term->slug; ?>' <?php echo selected(sanitize_text_field($_GET['orderby']), $term->slug); ?>><?php echo $term->name; ?></option>
<?php endforeach; ?>
</select>
<button type='submit'>Filter</button>
</form>
您还缺少问题中的结束标签! </form>
.
我创建了一个带有自定义分类的自定义 post 类型。为此,我在 functions.php
//Custom Posttype
function custom_post_type_rental() {
// source: https://wordpress.stackexchange.com/questions/156978/custom-post-type-single-page-returns-404-error
$labels = array(
'name' => _x( 'Rentals', 'i2sa'),
'singular_name' => _x( 'rentals', 'i2sa'),
'menu_name' => _x( 'Rentals', 'i2sa'),
'name_admin_bar' => _x( 'Admin Bar', 'i2sa'),
'add_new' => _x( 'Nieuwe rental', 'i2sa'),
'add_new_item' => __( 'Voeg nieuwe rental toe', 'i2sa'),
'new_item' => __( 'Nieuw item', 'i2sa'),
'edit_item' => __( 'Bewerk rentals', 'i2sa'),
'view_item' => __( 'Bekijk rentals', 'i2sa'),
'all_items' => __( 'Alle rentals', 'i2sa'),
'search_items' => __( 'Zoek rentals', 'i2sa'),
'parent_item_colon' => __( 'Parent rentals:', 'i2sa'),
'not_found' => __( 'Zoekopdracht niet gevonden.', 'i2sa'),
'not_found_in_trash' => __( 'Zoekopdracht niet gevonden in prullenbak.', 'i2sa')
);
$args = array(
'labels' => $labels,
'description' => "Beschrijving i2sa",
'public' => true, //openbaar
'exclude_from_search'=> true, //uitschakelen voor zoekopdrachten
'publicly_queryable' => true, //publiekelijk vindbaar
'show_in_nav_menus' => false, //toon in navigatie
'menu_icon' => 'dashicons-bank',
'show_ui' => true,
'show_in_rest' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'rental' ), //custom url
'capability_type' => 'post',
'has_archive' => true, //heeft archivepage
'hierarchical' => false, // true maakt het een pagina
'menu_position' => null, //null = geen
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'author', 'trackbacks', 'comments', 'revisions', 'post-formats', 'page-attributes' ), //gesupporte functionaliteiten
//je kan de support 'thumbnail' ook vervangen door ‘attachment:audio’ and ‘attachment:video’
);
register_post_type( 'rental', $args );
//flush_rewrite_rules();
}
add_action( 'init', 'custom_post_type_rental' );
function rentalMachineSoort() {
//Labels voor de custom taxonomy
$labels = array(
'name' => _x( 'Machinesoorten', 'machinesoorten' ),
'singular_name' => _x( 'Machinesoort', 'machinesoort' ),
'search_items' => __( 'Search Machinesoorten' ),
'all_items' => __( 'All Machinesoorten' ),
'parent_item' => __( 'Parent Machinesoort' ),
'parent_item_colon' => __( 'Parent Machinesoort:' ),
'edit_item' => __( 'Edit Machinesoort' ),
'update_item' => __( 'Update Machinesoort' ),
'add_new_item' => __( 'Add New Machinesoort' ),
'new_item_name' => __( 'New Machinesoort Name' ),
'menu_name' => __( 'Machinesoorten' ),
);
// Taxonomy registreren
register_taxonomy('machinesoort',array('rental'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'machinesoort' ),
));
}
add_action( 'init', 'rentalMachineSoort', 0 );
在我的存档中,我想按此分类法过滤 post。为此,我使用了代码:
<form method="GET">
<select name="orderby" id="orderby">
<?php
$terms = get_terms([
'taxonomy' => 'machinesoort',
'hide_empty' => 'false'
]);
foreach ($terms as $term) :
?>
<option value="<?php echo $term->slug;?>" name="machinesoort[]"><?php echo $term->name;?></option>
<?php endforeach;?>
</select>
<button type="submit">Filter</button>
这会使页面刷新为包含参数的新 url。问题是页面仍然显示所有文章,包括不应该显示的文章。
任何人都知道我怎样才能让这段代码正常工作以便它能正确过滤?如果我只使用 checkboxxes 它工作正常,但我需要使用这个过滤器中的方法
"Problem is that the page still shows all articles, including the articles that should not be shown."
因为你错过了主要步骤!您没有根据所选值更改查询!
您可以使用 pre_get_posts
挂钩来完成。像这样:
add_action('pre_get_posts', 'altering_rental_archive_query', 99);
function altering_rental_archive_query($query)
{
if (
is_post_type_archive('rental')
&&
get_query_var('orderby')
)
{
$tax_query = array(
array(
'taxonomy' => 'machinesoort',
'field' => 'slug',
'terms' => sanitize_text_field(get_query_var('orderby')),
)
);
$query->set('tax_query', $tax_query);
};
};
结果如下:
在没有任何过滤器/查询变量的情况下首次加载存档页面时:
如果我按第一个自定义分类法过滤它:
如果我按第二个自定义分类法过滤它:
快一点 tip/bonus:
您可以将 selected
Docs 功能添加到 archive
页面上的表单,以便根据 url 上查询的变量正确选择过滤器下拉列表:
<form method='GET'>
<select name='orderby' id='orderby'>
<?php
$terms = get_terms([
'taxonomy' => 'machinesoort',
'hide_empty' => 'false'
]);
foreach ($terms as $term) :
?>
<option value='<?php echo $term->slug; ?>' <?php echo selected(sanitize_text_field($_GET['orderby']), $term->slug); ?>><?php echo $term->name; ?></option>
<?php endforeach; ?>
</select>
<button type='submit'>Filter</button>
</form>
您还缺少问题中的结束标签! </form>
.