Change the related listings query

I have 2 questions

  1. First question. I added custom select attribute called xcustom. And I want to change related listing query but it doesnt work. what i did wrong?
add_action( 'hivepress/v1/models/listing/relate', function( $query, $listing ) {

			// Set xcustom.
			if ( $listing->get_xcustom__id() ) {
				$query->filter( [ 'xcustom__in' => $listing->get_xcustom__id() ] );
			}

}, 10, 2 );

But if I replace it with this, it works. but the weird thing is we need to re add query for categories again:

    $query->set_args(
        [
            'tax_query' => [
                'relation' => 'AND',

                // Set xcustom.
                [
                    'taxonomy' => 'hp_listing_xcustom',
                    'field'    => 'term_id',
                    'terms'    => $listing->get_xcustom__id(),
                    'operator' => 'IN',
                ],

                // Set categories. we need this eventhough its already in 'hivepress/v1/models/listing/relate'. maybe its hivepress bug
                [
                    'taxonomy' => 'hp_listing_category',
                    'field'    => 'term_id',
                    'terms'    => $listing->get_categories__id(),
                    'operator' => 'IN',
                ],
            ],
        ]
    );

Seems after reading this, its a bug?

  1. Second question. How to make it also search for listing by the same author/vendor using hivepress query model?

Hi,

  1. If the attribute type is Select and it doesn’t allow multiple selection, you can try this first:
$query->filter([
    'xcustom' => 123,
]);

Replace 123 with the ID of an existing option of this Select attribute (you can check these via the Edit Options button, option ID is in the URL when you edit it, something like ā€œā€¦tag_ID=123ā€¦ā€). This way you can check if the query works at all, and if so you can try to provide a variable value $listing->get_xcustom__id() or debug this further.
2. You can try this filter:

$query->filter([
    'user' => $listing->get_user__id(),
]);

Hope this helps

  1. Its still doesnt work. Should I put it in bug section? I’m very sure its a bug, as the other user from the thread i sent above also failed using the same technique.

  2. I want to show related listing from the same current listing’s owner. your code works. but im curious what is the difference between this 2 code, as using either of those work. which one is more preferable:

$query->filter(['user' => $listing->get_user__id()]);
OR
$query->filter(['vendor' => $listing->get_vendor__id()]);
  1. when using set_args, why we need to add categories rule again? without that it will show all listing from all categories. that rule already added in ā€˜/includes/blocks/class-related-listings.php’ line 76
    $query->set_args(
        [
            'tax_query' => [
                'relation' => 'AND',

                // Set xcustom.
                [
                    'taxonomy' => 'hp_listing_xcustom',
                    'field'    => 'term_id',
                    'terms'    => $listing->get_xcustom__id(),
                    'operator' => 'IN',
                ],

                // Set categories. we need this eventhough its already in 'hivepress/v1/models/listing/relate'. maybe its hivepress bug
                [
                    'taxonomy' => 'hp_listing_category',
                    'field'    => 'term_id',
                    'terms'    => $listing->get_categories__id(),
                    'operator' => 'IN',
                ],
            ],
        ]
    );
  1. Please try to debug this via var_dump($query->get_args()); This way you can check how the WP query changes after you add the custom attribute filter, and if it changes at all.
  2. Either is ok, but user can have multiple vendor profiles in future updates, so filtering by user ID may be more safe.
  3. Yes, this issue occurs because set_args overrides the query arguments tax_query and meta_query instead of merging them. You can merge them using $query->get_args()['tax_query'] to prevent duplicating arguments manually.

I cheked with $query->get_args() but it doesnt alter the query at all with this code:

$query->filter( [ 'xcustom__in' => $listing->get_xcustom__id() ] );

So filter doesnt work with ā€œselectā€ custom attribute?

It should be mentioned in the official docs here:

and second question:

I have another weird thing, here’s the code I use.

add_action( 'hivepress/v1/models/listing/relate', function( $query, $listing ) {

    var_dump( $query->get_args() );
    var_dump( $listing->get_xcustom__id() );
    var_dump( $listing->get_categories__id() );
    $query->set_args(
        [


            'tax_query' => [
                'relation' => 'AND',

                // Set xcustom.
                [
                    'taxonomy' => 'hp_listing_xcustom',
                    'field'    => 'term_id',
                    'terms'    => $listing->get_xcustom__id(),
                    'operator' => 'IN',
                ],

                // Set categories. we need this eventhough its already in 'hivepress/v1/models/listing/relate'. maybe its hivepress bug
                [
                    'taxonomy' => 'hp_listing_category',
                    'field'    => 'term_id',
                    'terms'    => $listing->get_categories__id(),
                    'operator' => 'IN',
                ],

            ],


        ]
        
    );
    var_dump( $query->get_args() );

}, 10, 2 );

but the terms for ā€˜hp_listing_xcustom’ misteriously change from array(6) to array(6,4), see the raw dumps below. From where that ā€˜4’ comes from??

Here’s the raw dump:

Array
(
    [post_status] => publish
    [posts_per_page] => 3
    [orderby] => rand
    [ignore_sticky_posts] => 1
    [post_type] => hp_listing
    [post__not_in] => Array
        (
            [0] => 18
        )

    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => hp_listing_category
                    [operator] => IN
                    [terms] => Array
                        (
                            [0] => 4
                        )

                )

        )

)

Array
(
    [0] => 6
)

Array
(
    [0] => 4
)

Array
(
    [post_status] => publish
    [posts_per_page] => 3
    [orderby] => rand
    [ignore_sticky_posts] => 1
    [post_type] => hp_listing
    [post__not_in] => Array
        (
            [0] => 18
        )

    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => hp_listing_xcustom
                    [operator] => IN
                    [terms] => Array
                        (
                            [0] => 4
                            [1] => 6
                        )

                    [field] => term_id
                )

            [relation] => AND
            [1] => Array
                (
                    [taxonomy] => hp_listing_category
                    [field] => term_id
                    [terms] => Array
                        (
                            [0] => 4
                        )

                    [operator] => IN
                )

        )

)

I also tried with only hivepress run and make sure only the code above run. But still same result

Please try to hard-code the ID without ā€œ__inā€ as suggested above and try to output the args:

$query->filter([
    'xcustom' => 123,
]);

Regarding the second issue, please var_dump $listing->get_xcustom__id(). If 4 is the category ID and it’s added automatically, please make sure that your function callback runs after all the other callbacks (change 10 priority to 1000), this may resolve the issue.

Thanks, we’ll try to improve the docs

Still not fixed the issue

CODE 1:
add_action( 'hivepress/v1/models/listing/relate', function( $query, $listing ) {

   var_dump( $query->get_args() );

   $query->filter([
    'xcustom' => 6,
    ]);

    var_dump( $query->get_args() );

}, 1000, 2 );
RESULT 1:
Array
(
    [post_status] => publish
    [posts_per_page] => 3
    [orderby] => rand
    [ignore_sticky_posts] => 1
    [post_type] => hp_listing
    [post__not_in] => Array
        (
            [0] => 18
        )

    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => hp_listing_category
                    [operator] => IN
                    [terms] => Array
                        (
                            [0] => 4
                        )

                )

        )

)

Array
(
    [post_status] => publish
    [posts_per_page] => 3
    [orderby] => rand
    [ignore_sticky_posts] => 1
    [post_type] => hp_listing
    [post__not_in] => Array
        (
            [0] => 18
        )

    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => hp_listing_category
                    [operator] => IN
                    [terms] => Array
                        (
                            [0] => 4
                        )

                )

        )

)

Yes its category id. I dont know why its added automagically. I tried to change the priority to 1000 and 9999 still doesnt work.

Also I notice, it only automatically add the category id if the terms query is array.
$listing->get_xcustom__id() is returning array.

$query->set_args(
    [
        
		'tax_query' => [
		    [
		        'taxonomy' => 'hp_listing_xcustom',
		        //'field'    => 'term_id',
		        'terms'    => array(6),
		        'operator' => 'IN',
		    ],
		],

    ]
    
);

I added this to the bug tracker, please try to set a high priority (1000) for the function callback and use $query->set_args() to fully override the tax_query as a temporary fix.

Thank you very much Ihor.

Just to confirm so there is 2 bugs right?

  1. $query->filter doesn’t work for ā€œselectā€ field in custom attribute:
   $query->filter([
    'xcustom' => 6,
    ]);
  1. $query->set_args() automatically add category_id if ā€˜terms’ is array (even if the priority is set to 1000):
$query->set_args(
    [
        
		'tax_query' => [
		    [
		        'taxonomy' => 'hp_listing_xcustom',
		        //'field'    => 'term_id',
		        'terms'    => array(6),
		        'operator' => 'IN',
		    ],
		],

    ]
    
);

Hi,

There’s a bug related to selectable attribute fields not being available at the time this hook fires, but there’s a solution for the second issue - arrays are merged so adding a custom key should prevent this (this way you can still set any arguments for WP_Query as a fallback). I tested this code with the ListingHive demo content and it worked:

add_action(
	'hivepress/v1/models/listing/relate',
	function ( $query, $listing ) {
		$query->set_args(
			[
				'tax_query' => [
					'my_custom_clause' => [
						'taxonomy' => 'hp_listing_condition',
						'terms'    => $listing->get_condition__id(),
					],
				],
			]
		);
	},
	1000,
	2
);

I can confirm it solved the second issue. Thank youšŸ™

1 Like