I am wanting to filter WordPress posts that have a specific block present. I have the initial tagging done and can see which posts have the block I am searching for.
I tried using the query to do the actual filtering but got lost as the documentation I can find only filters using metadata from things like on page drop down menus. Can I add that type of metadata to a block?
// Add Column to admin post list
function custom_columns_list( $columns ) {
$columns['blockBool'] = 'Block Present?';
return $columns;
}
add_filter( 'manage_post_posts_columns', 'custom_columns_list' );
// check post if block is present
function blockColumn() {
//using a block from learn.wordpress
if ( has_block( 'create-block/multi-columns' ) ) {
// echo '<p>True</p>';
$blockBool = "True";
echo "$blockBool";
} else {
// echo '<p>False</p>';
$blockBool = "False";
echo "$blockBool";
}
}
add_action( 'manage_posts_custom_column', 'blockColumn', 10, 2 );
// add filter drop down to post admin list
function blockCheckFilter( $post_type ) {
if ( $post_type === 'post' ) {
$blockBool = filter_input( INPUT_GET, 'blockBool' );
?>
<select name="blockBool">
<option value="">Block Present?</option>
<option value="True" <?php echo selected( 'True', $blockBool ); ?>>True</option>
<option value="False" <?php echo selected( 'False', $blockBool ); ?>>False</option>
</select>
<?php
}
}
add_action( 'restrict_manage_posts', 'blockCheckFilter', 10, 2 );
Here is where I got lost. I tried using $query->set to no positive results
//use query to filter block results?
function block_order_result( $query ) {
$blockBool = filter_input( INPUT_GET, 'blockBool' );
if ( is_admin() && ! empty( $blockBool ) ) {
switch ( $blockBool ) {
case 'True':
echo "Show posts where blockBool is true";
break;
case 'False':
echo "My brain hurts";
break;
default:
// code...
break;
}
}
}
add_action( 'pre_get_posts', 'block_order_result' );
function sort_column( $columns ) {
$columns['blockBool'] = 'blockBool';
return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'sort_column' );
Here is what my current admin page looks like.