• Resolved laiconsulting

    (@laiconsulting)


    I get a warning

    PHP Warning: Creating default object from empty value

    from the line starts with @ below.

    add_action( 'pre_get_posts', 'bv_query_exclude_private');
    function bv_query_exclude_private(&$query){
            if(!bv_member_privilege() && property_exists($query,'tax_query') ){
                    $tax_query= array(
                            'taxonomy' => 'category',
                            'include_children' => true,
                            'field'    => 'term_id',
                            'operator' => 'NOT IN',
                            'terms'    => array( 1 ),
                    );
                    @$query->tax_query->queries[] = $tax_query;
                    $query->query_vars['tax_query'] = $query->tax_query->queries;
            }
    }

    After some digging, it appears that I am using un-initialised $query and I should define $query = new WP_Query( $args ) within the function. Have I understood something wrong? How can I change the query to exclude term_id 1 in category?

    Best regards,
    Alan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @laiconsulting

    pre_get_posts hook already pass the query object as reference, so you can simply accept it without having to declare the definition of your function to accept variable directly.

    You wouldn’t create your own WP_Query() object because you are trying to modify an existing query and not create a separate query of your own.

    The function property_exists() you are using returns true even if the property has the value null so it can produce unintended side effects. So it would be better to check with isset() instead. That should fix the warning for you because right now I think its passing the if block when tax_query property is null and then upon accessing it $query->tax_query->queries, warning is thrown.

    Try this code:

    add_action( 'pre_get_posts', 'bv_query_exclude_private');
    function bv_query_exclude_private(&$query){
            if ( $query->is_main_query() && !bv_member_privilege() ) {
                    if ( ! isset( $query->tax_query ) ) {
                        $tax_query = [];
                    }
                    $tax_query[] = array(
                            'taxonomy' => 'category',
                            'include_children' => true,
                            'field'    => 'term_id',
                            'operator' => 'NOT IN',
                            'terms'    => array( 1 ),
                    );
                    $query->set('tax_query', $tax_query );
            }
    }
    Thread Starter laiconsulting

    (@laiconsulting)

    Hello @ashfame

    it worked. Many thanks for looking into my code and your suggestion.

    Best regards,
    Alan

    Great! I just noticed, the function defined could be just this:

    function bv_query_exclude_private($query){ without the &. Somehow it made to the snippet I provided you, even though I was trying to omit it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to do pre_get_posts action?’ is closed to new replies.