Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
    Tweeted twitter.com/StackWordPress/status/688227663782100993
2 deleted 2712 characters in body
source | link

My current simplified query arguments filter:

add_filter( 'post_one_search_args', 'my_post_one_search_args' );

function my_post_one_search_args( $search_results_args ) {


    $search_results_args['post_type'] = 'post-one';
    $search_results_args['post_status'] = 'publish';
    $search_results_args['paged'] = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $search_results_per_page = 10;

    $meta_query = array();
    $tax_query = array();


    /* Random arguments taken from my query -> there's total of over 20 but I'll 
       show you different types for the sake of this question */


    //Keyword search
    $search_results_args['s'] = $search_value; //$_GET from form input

    //Input number
    $meta_query[] = array(
        'key'           => 'post_one_price',
        'value'         => $search_value, //$_GET from form input
        'type'          => 'NUMERIC',
        'compare'       => '>='
    );

    //Date field
    $meta_query[] = array(
        'key'           => 'post_one_available_from',
        'value'         => $search_value, //$_GET from form input
        'type'          => 'DATE',
        'compare'       => '<='
    );  

    //Taxonomy
    if ( ! empty( $search_value ) ) {  //Dropdown -> empty means that option "all" is selected

        $tax_query[] = array(
            'taxonomy'      => 'post-one-location',
            'field'         => 'slug',
            'terms'         => $search_value   //$_GET from form input
        );
    }

    //Counts
    $meta_count = count( $meta_query );
    $tax_count = count( $tax_query );


    //Relations
    if ( $meta_count > 1 ) {
        $meta_query['relation'] = 'AND';
    }
    if ( $tax_count > 1 ) {
        $tax_query['relation'] = 'AND';
    }


    //Declare meta and tax arguments if there's any
    if ( $meta_count > 0 ) {
        $search_results_args['meta_query'] = $meta_query;
    }
    if ( $tax_count > 0 ) {
        $search_results_args['tax_query'] = $tax_query;
    }

    $search_results_args['orderby'] = array('date' => 'DESC');

    return $search_results_args;

}


//How it's used in different files
$search_results_args = array();
$search_results_args = apply_filters( 'post_one_search_args', $search_results_args );


//Plus I sometimes add extra arguments in here e.g if I want to show only user's posts
$search_results_args['author'] = $current_user->ID;


$query_search_results = new WP_Query( $search_results_args );

My current simplified query arguments filter:

add_filter( 'post_one_search_args', 'my_post_one_search_args' );

function my_post_one_search_args( $search_results_args ) {


    $search_results_args['post_type'] = 'post-one';
    $search_results_args['post_status'] = 'publish';
    $search_results_args['paged'] = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $search_results_per_page = 10;

    $meta_query = array();
    $tax_query = array();


    /* Random arguments taken from my query -> there's total of over 20 but I'll 
       show you different types for the sake of this question */


    //Keyword search
    $search_results_args['s'] = $search_value; //$_GET from form input

    //Input number
    $meta_query[] = array(
        'key'           => 'post_one_price',
        'value'         => $search_value, //$_GET from form input
        'type'          => 'NUMERIC',
        'compare'       => '>='
    );

    //Date field
    $meta_query[] = array(
        'key'           => 'post_one_available_from',
        'value'         => $search_value, //$_GET from form input
        'type'          => 'DATE',
        'compare'       => '<='
    );  

    //Taxonomy
    if ( ! empty( $search_value ) ) {  //Dropdown -> empty means that option "all" is selected

        $tax_query[] = array(
            'taxonomy'      => 'post-one-location',
            'field'         => 'slug',
            'terms'         => $search_value   //$_GET from form input
        );
    }

    //Counts
    $meta_count = count( $meta_query );
    $tax_count = count( $tax_query );


    //Relations
    if ( $meta_count > 1 ) {
        $meta_query['relation'] = 'AND';
    }
    if ( $tax_count > 1 ) {
        $tax_query['relation'] = 'AND';
    }


    //Declare meta and tax arguments if there's any
    if ( $meta_count > 0 ) {
        $search_results_args['meta_query'] = $meta_query;
    }
    if ( $tax_count > 0 ) {
        $search_results_args['tax_query'] = $tax_query;
    }

    $search_results_args['orderby'] = array('date' => 'DESC');

    return $search_results_args;

}


//How it's used in different files
$search_results_args = array();
$search_results_args = apply_filters( 'post_one_search_args', $search_results_args );


//Plus I sometimes add extra arguments in here e.g if I want to show only user's posts
$search_results_args['author'] = $current_user->ID;


$query_search_results = new WP_Query( $search_results_args );
1
source | link

Compile two queries to one set of results?

I've got a big query that queries one post_type with all different kinds of filters. Values are taken from <form> inputs.

  • Im adding 3 different checkboxes to the <form> which can be used as post_type toggle - this means that you can query only one post_type of the three, two of them or all at once
  • Every post_type has it's own inputs that I show / hide with jQuery
  • post_type inputs should only affect / filter its own post_type
  • All the results must be displayed in same list

My question is: how to query different post_types when each have its own arguments?


Thoughts after few days of thinking about this:

  • Three different queries (poor, poor performance) and compile multiple queries into one loop?

    • While Im having tons of query arguments, it should drastically speed up query because there's pretty narrow scope, at least my logic says it and three different queries shouldn't be that bad even if each have thousands of posts?

    • My only concern is that user can also query without filling any inputs which means that all three queries are quering ALL the posts.

  • Including several post_types seems impossible - you cannot use different metas and taxes on different post_types using conditional logic in same query, right?


My current simplified query arguments filter:

add_filter( 'post_one_search_args', 'my_post_one_search_args' );

function my_post_one_search_args( $search_results_args ) {


    $search_results_args['post_type'] = 'post-one';
    $search_results_args['post_status'] = 'publish';
    $search_results_args['paged'] = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $search_results_per_page = 10;

    $meta_query = array();
    $tax_query = array();


    /* Random arguments taken from my query -> there's total of over 20 but I'll 
       show you different types for the sake of this question */


    //Keyword search
    $search_results_args['s'] = $search_value; //$_GET from form input

    //Input number
    $meta_query[] = array(
        'key'           => 'post_one_price',
        'value'         => $search_value, //$_GET from form input
        'type'          => 'NUMERIC',
        'compare'       => '>='
    );

    //Date field
    $meta_query[] = array(
        'key'           => 'post_one_available_from',
        'value'         => $search_value, //$_GET from form input
        'type'          => 'DATE',
        'compare'       => '<='
    );  

    //Taxonomy
    if ( ! empty( $search_value ) ) {  //Dropdown -> empty means that option "all" is selected

        $tax_query[] = array(
            'taxonomy'      => 'post-one-location',
            'field'         => 'slug',
            'terms'         => $search_value   //$_GET from form input
        );
    }

    //Counts
    $meta_count = count( $meta_query );
    $tax_count = count( $tax_query );


    //Relations
    if ( $meta_count > 1 ) {
        $meta_query['relation'] = 'AND';
    }
    if ( $tax_count > 1 ) {
        $tax_query['relation'] = 'AND';
    }


    //Declare meta and tax arguments if there's any
    if ( $meta_count > 0 ) {
        $search_results_args['meta_query'] = $meta_query;
    }
    if ( $tax_count > 0 ) {
        $search_results_args['tax_query'] = $tax_query;
    }

    $search_results_args['orderby'] = array('date' => 'DESC');

    return $search_results_args;

}


//How it's used in different files
$search_results_args = array();
$search_results_args = apply_filters( 'post_one_search_args', $search_results_args );


//Plus I sometimes add extra arguments in here e.g if I want to show only user's posts
$search_results_args['author'] = $current_user->ID;


$query_search_results = new WP_Query( $search_results_args );