admin管理员组

文章数量:1279055

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I want to add a complex filtering when getting my products.

  • I have products where some of them are linked to a custom post type.
  • This custom post type has a repeater field.
  • Depending on a cookie, I should only get products that have that value in this repeater field

I know I can use:

add_filter( 'pre_get_posts', 'fwp_archive_per_page' );

I know i can set extra meta query like this:

$query->set( 'meta_query', array(
    'relation'    => 'AND',
        array(
            'key'   => 'xxx',
            'value'     => 'xxx'
        )
));

But this is more complex as I should do a query on every product ID to check if the value exists in the repeater field from the related custom post type.

Anyone has a clue how to do this?
Or is there another way around to handle this?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I want to add a complex filtering when getting my products.

  • I have products where some of them are linked to a custom post type.
  • This custom post type has a repeater field.
  • Depending on a cookie, I should only get products that have that value in this repeater field

I know I can use:

add_filter( 'pre_get_posts', 'fwp_archive_per_page' );

I know i can set extra meta query like this:

$query->set( 'meta_query', array(
    'relation'    => 'AND',
        array(
            'key'   => 'xxx',
            'value'     => 'xxx'
        )
));

But this is more complex as I should do a query on every product ID to check if the value exists in the repeater field from the related custom post type.

Anyone has a clue how to do this?
Or is there another way around to handle this?

Share Improve this question edited Nov 3, 2021 at 10:32 Buttered_Toast 2,8191 gold badge8 silver badges21 bronze badges asked Nov 3, 2021 at 10:22 jens_vdpjens_vdp 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

When I have complex querie to do, I often use a prequery to get the good post_ids. Then I pass this list of post_ids to my main query via the post__in parameter of WP_Query.

$meta_key = 'RepeaterName_%_FieldName';
$meta_value = 'MyCustomValue';

global $wpdb;
$post_ids = $wpdb->get_col( $wpdb->prepare( 
    "
        SELECT      pm.post_id
        FROM        $wpdb->postmeta pm
        WHERE       pm.meta_key LIKE %s 
        AND         pm.meta_value = %s
    ",
    $meta_key, 
    $meta_value, 
) ); 
// Or with another custom $wpdb request 
// https://developer.wordpress/reference/classes/wpdb/

Then we add $post_ids to main query

if ( !empty( $post_ids ) ) {
    $post__in = $query->get('post__in');
    if ( empty( $post_in ) ) {
        $query->set( 'post__in', $post_ids );
    } else {
        $query->set( 'post__in', array_intersect( $post_ids, $post__in ) )
    }
}

All this code must be placed in the pre_get_posts filter. I hope that the response is up to your expectations.

本文标签: loopWoocommerce Complex query in pregetposts