[Resolved] element rule not working on faq category page

Home Forums Support [Resolved] element rule not working on faq category page

Home Forums Support element rule not working on faq category page

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2031369
    Paul

    I have 2 element rules that should be applicable to the following page but do not seem to work:
    https://www.suddencardiacarrestuk.org/faq-category/travelling/

    The first rule “Breadcrumb” is to disable the yoast breadcrumb from the page.
    The second is to add the “Faq breadcrumb” to the page.

    These rules work on the individual faq question page, such as
    https://www.suddencardiacarrestuk.org/faq-questionimplantable-devices/are-there-any-medications-that-defibrillator-patients-should-not-take/

    I don’t know if it’s to do with the way the faq plugin creates the category pages or not, but when I set the rules up I have to specify it as
    “FAQ Question FAQ Categories” and on the right option select an actual category, such as “Treatment” or “Travelling”. Ideally I’d like to be able to just say that it applies to all the category pages (as per the FAQ Question rules where I have an option “All FAQ Question”) but at the moment I would have to create a rule for every category that I create – which doesn’t;t seem right. However, I’m not sure why this is, the way the faq plugin is designed or the way generatepress is.

    Thanks

    #2031439
    David
    Staff
    Customer Support

    Hi there,

    that FAQ page isn’t a category archive, their using Page hierarchies.
    You can use the snippet here to make an Element apply to a All Children of the given Parent page.

    https://docs.generatepress.com/article/generate_element_display/#display-an-element-to-a-parent-page-and-all-child-pages

    #2031648
    Paul

    ok, how do I add this?

    Is it a hook?

    #2031718
    David
    Staff
    Customer Support

    Its PHP – this doc explains how to add:

    https://docs.generatepress.com/article/adding-php/

    #2032018
    Paul

    OK thanks. I already had Code Snippets installed and I added the following:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 13084 === $element_id && ( is_page() && $post->post_parent == '8971' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    Where I think 13084 is the element id (I assume the post Id when editing the element) and 8971 is the page id (again, from the URL when editing).

    I activated it and set it to run only on the frontend.

    The element Display Rules are set with the Location as “FAQ Question->All FAQ Questions” and “Page->FAQ”

    Not sure if this is how you meant but it doesn’t work for me.

    #2032113
    Ying
    Staff
    Customer Support

    Hi Paul,

    Can you try this one:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 13084 === $element_id && ( is_page( 8971 )) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    Let me know 🙂

    #2032148
    Paul

    Hi Ying,

    Unfortunately, this didn’t work either.

    Paul

    #2032204
    Ying
    Staff
    Customer Support

    The FAQ category pages and the FAQ pages are using the same ID, I don’t see parent-child relationship between them either.

    It’s a really strange structure.

    How did you add this breadcrumbs?
    https://www.screencast.com/t/CSd1AyO4Dp

    #2032247
    Paul

    I don’t know about the parent/child relationship, it’s what David suggested.

    I have a hook that outputs the yoast breadcrumbs, but that’s not what I want for the category pages. I’ve achieved what I want on the other FAQ pages, i.e. using the FAQ breadcrumb

    #2032274
    Ying
    Staff
    Customer Support

    I have a hook that outputs the yoast breadcrumbs

    What’s the location setting in this hook element?

    #2032334
    Paul

    It’s for the entire site but there are the following exclusions

    front page
    page -> faq all
    faq question ->all faq questions
    page -> faq

    and for the faq pages it should show a replacement faq breadcrumb with the following location rules…

    faq question -> all faq questions
    page -> faq

    #2032479
    Elvin
    Staff
    Customer Support

    Hi there,

    I don’t know if it’s to do with the way the faq plugin creates the category pages or not,

    You may be onto something with this. The page isn’t a archive page so we can’t use is_archive() and is_category(). The page source tells it’s a page (or atleast using a page template) but is_page() isn’t working.

    Can you ask the plugin’s developer what condition they use for generating category pages?

    We can use that condition for the display rule filter. 🙂

    #2038144
    Paul

    I had a reply back from the FAQ plugin developer…

    “The “main” page it’s just a regular wp page (post_type=”page”) with the [cm_faq] shortcode
    The category page, is, actually, the same entity (same wp page), but with rewritten global page query
    We use parse_query hook for that. Look for pre_get_posts() method In plugin code to see which parameters were changed by CMFAQ plugin

    The question page it’s just a regular single post page”

    I think this is the method they are referring to…

    public static function pre_get_posts(&$wp_query_ref) {
            global $wp_query;
            if (is_admin()) {
                return;
            }
            $is_tax = $wp_query_ref->is_main_query() && (is_tax('cmfaq_category') || is_tax('cmfaq_tag'));
            $is_search = $wp_query_ref->is_search() && get_query_var('post_type') == 'cmfaq_question';
            if ($is_tax || $is_search) {
                $wp_query_ref->set('order', Options::get_option('cmfaq_order'));
                $wp_query_ref->set('orderby', Options::get_option('cmfaq_orderby'));
                if ( Options::get_option('cmfaq_orderby') == 'voting' ) {
                    $wp_query_ref->set('meta_key', 'cmfaq_rating');
                    $wp_query_ref->set('orderby', array('meta_value_num' => Options::get_option('cmfaq_order'), 'title' => 'ASC'));
                }
                if ($is_tax && is_tax('cmfaq_category')) {
                    new TermOrder();
                    $arr_id = (array) get_term_meta($wp_query_ref->queried_object_id, 'cmfaq_list');
                    if (count($arr_id)) {
                        $terms = get_terms('cmfaq_list', array('include' => $arr_id, 'hide_empty' => FALSE));
                        foreach ((array) $terms as $term) {
                            if (!static::$homePageId) {
                                static::$homePageId = intval(get_term_meta($term->term_id, 'cmfaq_page_id', TRUE));
                            }
                        }
                    }
                }
                if ($is_tax && is_tax('cmfaq_tag')) {
                    new TermOrder();
                    $terms = (array) get_posts(array(
                                'post_type' => 'cmfaq_question',
                                'tax_query', array(
                                    array(
                                        'taxonomy' => 'cmfaq_tag',
                                        'field' => 'term_id',
                                        'terms' => $wp_query_ref->queried_object_id
                                    )
                                )
                    ));
                    $arr_id = array();
                    foreach ($terms as $term) {
                        foreach ((array) wp_get_post_terms($term->ID, 'cmfaq_category', array('fields' => 'ids')) as $cat_id) {
                            $arr_id = array_merge($arr_id, (array) get_term_meta($cat_id, 'cmfaq_list'));
                        }
                    }
                    $arr_id = array_unique($arr_id);
                    if (count($arr_id)) {
                        $terms = get_terms('cmfaq_list', array('include' => $arr_id, 'hide_empty' => FALSE));
                        foreach ((array) $terms as $term) {
                            if (!static::$homePageId) {
                                static::$homePageId = intval(get_term_meta($term->term_id, 'cmfaq_page_id', TRUE));
                            }
                        }
                    }
                }
                if ($is_search) {
                    $cmfaq_list_id = intval(filter_input(INPUT_GET, 'cmfaq_list_id'));
                    if ($cmfaq_list_id) {
                        static::$homePageId = intval(get_term_meta($cmfaq_list_id, 'cmfaq_page_id', TRUE));
                        $wp_query_ref->set('tax_query', array(
                            array(
                                'taxonomy' => 'cmfaq_category',
                                'field' => 'term_id',
                                'terms' => static::getCategoryIdArrForListId($cmfaq_list_id),
                            )
                        ));
                    }
                }
                if (!static::$homePageId) {
                    static::$homePageId = Options::get_option('cmfaq_page_id', -1);
                }
                if (static::$homePageId && get_post(static::$homePageId) && ($is_tax || $is_search)) {
                    self::$wp_query_taxonomy = $wp_query;
                    self::$wp_query_taxonomy->set('posts_per_page', -1);
                    self::$wp_query_taxonomy->set('nopaging', true);
                    self::$wp_query_page = new \WP_Query(array('page_id' => static::$homePageId));
                }
            }
        }

    Hopefully this makes sense to you?

    #2039432
    Elvin
    Staff
    Customer Support

    Can you try this?

    add_filter( 'generate_element_display', function( $display, $element_id ) {
    	$cmfaq_term = 75;
    	$cmfaq_cat = CMFAQ_Base::$wp_query_taxonomy->queried_object_id;
        if ( 389970 === $element_id && $cmfaq_term === $cmfaq_cat ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    Change the value of $cmfaq_term = 75; to the ID of the CM FAQ category term you’re trying to display the GP Element on.

    You’ll find the CM FAQ category term on the URL bar when you’re editing the category as shown here – https://share.getcloudapp.com/Kou7bQPr

    #2042211
    Paul

    Thanks, but the trouble with that is, I’d have to specify and update the code for every category I have, which may change in time.

    However, I did come up with the following which seems to work…

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 13110 === $element_id && strpos($_SERVER['REQUEST_URI'], 'faq-category') !== false ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 13110 === $element_id && strpos($_SERVER['REQUEST_URI'], 'faq-tag') !== false ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.