Support » Plugin: WP LIST PAGES BY CUSTOM TAXONOMY » Auto Populate Taxonomy Term on Which to Filter

  • Resolved mlkbj05

    (@mlkbj05)


    Thank you for this plugin!

    I am curious if it provides the ability to auto-populate the term on which to filter the list based on the terms applied to the page on which the list is displayed.

    For example, my site has pages which describe Ministries and a CPT for Projects. I am using a custom taxonomy to categories the ministries described by both the Ministry pages as well as the Projects.

    On a given Ministry page (say, Category=Women’s Ministry), I want to list all projects with the same category. This way the widget in my Ministry Sidebar can dynamically select the appropriate projects based on the category of the active Ministry page.

    Thanks for you thoughts on this…

    https://wordpress.org/plugins/wp-list-pages-by-custom-taxonomy/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author piccart

    (@piccart)

    Hello,

    sorry for the delay but I’ve missed this post.. I whish there was a better notification system for new posts..

    in any case, that feature is not included in the plugin, but could be an idea which I might work on. thanks! 🙂

    in the meanwhile, you could easily tweak the code to make it work for you.

    first of all, duplicate the file:
    plugins/wp-list-pages-by-custom-taxonomy/templates/pbytax_template.php

    and place it into your theme folder.
    then edit this new file in your theme, and look for this bit of code (near line 100)

    // add the taxonomy query to the args
    		$pbytax_args['tax_query'] = array(
    			 array(
    				'taxonomy' => $taxonomy,
    				'field' => 'id',
    				'terms' => $filter_term_id,
    				'operator' => 'IN'
    				)
    			);

    just above that piece of code, you should add this bit here:

    if ($filter_term_id==15){
    			$filter_term_id = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "ids"));
    		}

    in order to make it work, you must:

    – create a placeholder term within the same taxonomy that you want to use, and place its id in place of where you can see
    15 in the code above

    – change ‘my_taxonomy’ with the slug of the custom tax

    – in the widget, select to list the product post types, select the taxonomy, and then select the placeholder term.
    then the code will not consider that term, and instead grab posts from the categories of the current page.
    the placeholder is just so the above code will run only for widgets where you select that term, and you can still use normal widgets if you want.

    cheers!

    Plugin Author piccart

    (@piccart)

    Hi there!

    since version 1.4.0 there is an easier way to achieve this behaviour.

    you no longer need to create a customized version of the template file.

    so now you can follow all the above directions, but:
    – don’t create a duplicate of the pbytax_template.php
    – put this code into your functions.php

    add_filter( ‘pbytax_prepare_instance’, ‘pbytax_filter_by_current_term’, 11, 3 );
    function pbytax_filter_by_current_term( $new_instance, $instance, $widget_num ){
    global $post;
    if ($instance[‘filter_term’] == array(“15”) ){
    $new_instance[‘filter_term’] = wp_get_post_terms($post->ID, ‘my_taxonomy’, array(“fields” => “ids”));
    }
    return $new_instance;
    }

    ALTERNATIVE METHOD
    alternatively, you could skip all the passages for the creation of a placeholder term, and rather hook this behaviour only to a specific widget instance.
    in order to see which is the number of the pbytax widget in which you want to hook this functionality, you can right-click on the widget heading-bar from the Appearance>Widgets section.
    then select “inspect element” (or something similar depending on your browser).
    it will then display the page code in the bottom of your screen. it should highlight the currently clicked element which will have an id like this:
    id="widget-66_pages_by_tax-4"
    that last number is your widget instance number. in this case is 4, so you can then paste this code in your functions.php and it will be applied only to that very widget:

    add_filter( ‘pbytax_prepare_instance’, ‘pbytax_filter_by_current_term’, 11, 3 );
    function pbytax_filter_by_current_term( $new_instance, $instance, $widget_num ){
    global $post;
    if ( $widget_num == “4” ){
    $new_instance[‘filter_term’] = wp_get_post_terms($post->ID, ‘my_taxonomy’, array(“fields” => “ids”));
    }
    return $new_instance;
    }

    remember to change “my_taxonomy” with the slug of the taxonomy for which you want to filter. and you must select that taxonomy in the widget’s settings too.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Auto Populate Taxonomy Term on Which to Filter’ is closed to new replies.