• I have a product page with Product Filters active on the sidebar. I am trying to add a custom meta query in the functions.php but everytime I do, the Filters in the sidebar go missing. This is the query that I am setting:

    $meta_query = $q->get( ‘meta_query’ );
    $meta_query[] = array( ‘key’ => ‘user_role_visibility’, ‘compare’ => ‘LIKE’, ‘value’ => $user_role );
    $q->set( ‘meta_query’, $meta_query );

    Can you advise what may be the reason why the Filters are missing? Thanks!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Dmytro Holovnia

    (@dholovnia)

    Hi,

    Probably your query is influencing our filters query. You need to verify where you custom code is adding meta_query to.

    You may want to use Query Monitor plugin to check where your meta_query is added to.

    Regards,
    Dmytro

    Thread Starter roniewebstudio17

    (@roniewebstudio17)

    Hi Dmytro,

    Thanks for the reply. I was able to get the following query using Query Monitory plugin below:

    SELECT COUNT( DISTINCT wp_1d01165959_posts.ID ) as term_count, MAX(term_relationships.term_taxonomy_id) as term_count_id
    FROM wp_1d01165959_posts
    INNER JOIN wp_1d01165959_postmeta AS mt1
    ON ( wp_1d01165959_posts.ID = mt1.post_id )
    INNER JOIN wp_1d01165959_term_relationships AS term_relationships
    ON wp_1d01165959_posts.ID = term_relationships.object_id
    WHERE wp_1d01165959_posts.post_type IN ( ‘product’ )
    AND wp_1d01165959_posts.post_status = ‘publish’
    AND ( wp_1d01165959_posts.ID NOT IN (
    SELECT object_id
    FROM wp_1d01165959_term_relationships
    WHERE term_taxonomy_id IN (23) ) )
    AND ( ( wp_1d01165959_postmeta.meta_key = ‘user_role_visibility’
    AND wp_1d01165959_postmeta.meta_value LIKE ‘%Customer%’ )
    AND ( mt1.meta_key = ‘user_role_visibility’
    AND mt1.meta_value LIKE ‘%Customer%’ ) )
    AND term_relationships.term_taxonomy_id IN (244,242,264,266,245,243,247,246,265)
    GROUP BY term_relationships.term_taxonomy_id

    There is an error:

    Unknown column ‘wp_1d01165959_postmeta.meta_key’ in ‘where clause’

    There is a duplicate condition where one condition is using an alias (mt1) and another condition is accessing the column directly wp_1d01165959_postmeta.meta_key.

    Do you know how can this be rectified? Thanks!

    Plugin Author Dmytro Holovnia

    (@dholovnia)

    Hi,

    You need to have own postmeta entity with

    INNER JOIN wp_1d01165959_postmeta AS pm_my
    ON ( wp_1d01165959_posts.ID = pm_my.post_id )

    and

    AND ( ( pm_my.meta_key = 'user_role_visibility'
    AND pm_my.meta_value LIKE '%Customer%' )

    This way it will be working.

    Or at least add it to the FROM. You have selection from tables ‘posts’, ‘postmeta AS mt1’ and ‘term_relationships AS term_relationships’. You are trying to get values from the table postmeta while it is not in the list. postmeta in the request is attached as an mt1.

    Regards,
    Dmytro

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter Missing When Adding Custom Meta Query’ is closed to new replies.