Giter VIP home page Giter VIP logo

Comments (1)

Robbertdk avatar Robbertdk commented on June 26, 2024

You can now pass an id to the shortcode and get that id in the filter hook.

for example

[ajax_filter_posts id="only-vegan-recipes"]
/**
 * Add the diet term on all the queries made with the shortcode ajax_filter_posts
 *
 * @param array $query_args 			query arguments set by the plugin Ajax Filter posts
 * @param array $shortcode_attributes 	all shortcode attributes
 *
 * @return array a updated list of query arguments
 */
function my_site_set_additional_term_for_ajax_filter_posts($query_args, $shortcode_attributes) {
    
    // Only show posts with the term vegan in the diet taxonomy for the shortcode with the id only-vegan-recipes
    if (!empty($shortcode_attributes['id']) && $shortcode_attributes['id'] === 'only-vegan-recipes') {
        $diet_tax_query_args = [
            [
                'taxonomy' => 'diet',
                'field'    => 'slug',
                'terms'    => 'vegan',
            ],
        ];

        // If there are already tax queries args set, merge my query args with the set args
        if ( !empty( $query_args['tax_query'] ) ) {
            $prev_set_tax_args = $query_args['tax_query'];
            $query_args['tax_query'] = [
                // Set the relationship to AND: we want only post with my term and the set terms by the user
                // Also see https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
                'relation' => 'AND',
                $diet_tax_query_args,
                $prev_set_tax_args
            ];
            return $query_args;
        }

        // If there are no tax queries args already set, just add it
        $query_args['tax_query'] = $diet_tax_query_args; 
    }
	
	return $query_args;
}
add_filter('ajax_filter_posts_query_args', 'my_site_set_additional_term_for_ajax_filter_posts', 10, 2);

from wordpress-ajax-filter-posts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.