ryanwelcher / advanced-query-loop

WordPress plugin that introduces a Query Loop block variation that can create advanced queries.
87 stars 10 forks source link

FEATURE: Add exclude category #83

Open ghost-ng opened 1 week ago

ghost-ng commented 1 week ago

Would be great to add an option to exclude categories from the query results

requires: autoload available category empty field for excluded categories while typing, autocomplete available categories auto transform matched category into bubble with "x" to remove the category auto refresh the results

ghost-ng commented 6 days ago

This is my workaround - I convert all categories into meta strings so I can then use for the meta queries. I have it as a plugin on my site, works great until this feature is added.

<?php
/**
 * Plugin Name: Save Categories as Meta Keys
 * Description: This plugin automatically saves the post's categories as a comma-separated string in a custom meta field ('category_meta') whenever the post is saved or updated.
 * Version: 1.2
 * Author: ghost-ng
 * License: GPLv2 or later
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

// Function to save the current categories (retaining case) in the 'category_meta' field every time a post is saved or updated.
function apmc_update_post_categories_as_meta( $post_id ) {
    // Make sure it's not an autosave or a revision.
    if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
        return;
    }

    // Get the post categories.
    $categories = get_the_category( $post_id );

    if ( ! empty( $categories ) ) {
        // Collect the category names (to retain letter case).
        $category_names = wp_list_pluck( $categories, 'name' ); // Use 'name' to retain the original case

        // Join the current category names into a comma-separated string.
        $categories_string = implode( ', ', $category_names ); // Add a space after commas for readability

        // Update the post meta with the new category names string, replacing the old one.
        update_post_meta( $post_id, 'category_meta', $categories_string );
    } else {
        // If no categories exist, delete the meta to reflect that.
        delete_post_meta( $post_id, 'category_meta' );
    }
}

// Hook into the 'save_post' action to update the categories in the meta field when the post is saved.
add_action( 'save_post', 'apmc_update_post_categories_as_meta' );

// Function to check all posts and update the 'category_meta' field on plugin activation.
function apmc_update_category_meta_for_all_posts() {
    // Query all posts.
    $all_posts = new WP_Query( array(
        'post_type'      => 'post',
        'posts_per_page' => -1, // Get all posts
        'post_status'    => 'publish',
    ) );

    // Loop through all posts and update the category_meta field.
    if ( $all_posts->have_posts() ) {
        while ( $all_posts->have_posts() ) {
            $all_posts->the_post();
            $post_id = get_the_ID();
            apmc_update_post_categories_as_meta( $post_id );
        }
    }

    // Reset post data.
    wp_reset_postdata();
}

// Run the function when the plugin is activated to update all existing posts.
register_activation_hook( __FILE__, 'apmc_update_category_meta_for_all_posts' );