当我注册我的自定义帖子时,它会向WP Admin仪表板添加一个菜单选项,用户可以单击该菜单选项,并列出该类型的所有自定义帖子。 (如果我对所描述的页面不了解,请在下面看到我的图像。)>
该页面列出了该类型的所有自定义帖子,并且效果很好,但是我只希望该页面显示某些
自定义帖子。我只想显示其post_metamy_flag
等于true的自定义帖子。我的所有自定义帖子的my_flag
post_meta设置为true或false。我希望我的列表页仅显示my_flag
= true的自定义帖子。 注册自定义帖子类型时可以这样做吗?即使不是,我将如何实现呢?
$labels = array( 'name' => _x( '_foo', 'Post Type General Name', '_foo' ), 'singular_name' => _x( 'Foo', 'Post Type Singular Name', '_foo' ), 'menu_name' => __( '_foo', '_foo' ), 'parent_item_colon' => __( 'Parent _foo', '_foo' ), 'all_items' => __( 'Foo Posts', '_foo' ), 'view_item' => __( 'View Foo', '_foo' ), 'add_new_item' => __( 'Add New Foo', '_foo' ), 'add_new' => __( 'Add New', '_foo' ), 'edit_item' => __( 'Edit Foo', '_foo' ), 'update_item' => __( 'Update Foo', '_foo' ), 'search_items' => __( 'Search Foo', '_foo' ), 'not_found' => __( 'Not Found', '_foo' ), 'not_found_in_trash' => __( 'Not found in Trash', '_foo' ), ); $args = array( 'label' => __( 'foo', '_foo' ), 'description' => __( 'My custom posts', '_foo' ), 'labels' => $labels, 'supports' => array( 'title', 'revisions', 'taxonomies' => array( 'foo' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => 'my-plugin-menu', 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'show_in_rest' => true, 'query_var' => true, 'menu_position' => 5, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'page' ); register_post_type('foo', $args);
我像这样设置post_meta:
add_post_meta($post_id, 'my_flag', true, true);
当我注册我的自定义帖子时,它会向WP Admin仪表板添加一个菜单选项,用户可以单击该菜单选项,并列出该类型的所有自定义帖子。 (如果我没有道理,请在下面查看我的图片...
您可以编辑wp_query,它将从数据库中获取帖子列表。请尝试以下功能: