Skip to main content
added 269 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

First, you are setting $_GET['filterN'] but you are trying to use $_GET['condition_N']. That is not ever going to work right.

Secondly, pagename is an "exact match" value, not a search parameter, and it only works with the "Page" post type (at least so far as I can tell). You are using a CPT and, presumably, you do not want an exact match. If you did the other query conditions would pointless. So that isn't going to work correctly either.

The simplest thing you can do is the use the s parameter.

$title = $_GET['s'];esc_textarea( $_GET['s'] );
$args = array(
    's'           => $title,
    'numberposts' => -1,
    'post_type'   => 'my_custom_post',
    'meta_query'  => array(
        array(
            'key'   => 'filter_1',
            'value' => esc_attr( $_GET['filter1'] )
        ),
        array(
            'key'   => 'filter_2',
            'value' => esc_attr( $_GET['filter2'] )
        ),
        array(
            'key'   => 'filter_3',
            'value' => esc_attr( $_GET['filter3'] )
        )
    )
);
$the_query = new WP_Query( $args );

That will search the post title for your $title string. It will also search post_content. To only search the title you will need to filter the search part of the query.

function only_title_search_wpse_119422( $search ) {
    remove_filter( 'posts_search', 'only_title_search_wpse_119422' ); 

    global $wpdb; 

    $pattern = "/\({$wpdb->posts}.post_title[^)]+\)/";
    preg_match_all( $pattern, $search, $matches ); 

    if ( ! empty( $matches[0] ) ) {
        $search = 'sprintf(
            " AND ('.%s)",
            implode(' " AND '", $matches[0] ).'
        )';;
    } 

    return $search;
}
add_filter( 'posts_search', 'only_title_search_wpse_119422' );

Add that immediately before your secondary query runs.

I can't tell for sure, but I wonder if you shouldn't be using a filter on pre_get_posts instead of the secondary query. The same basic ideas should apply in either case.

Note: Always use the esc_*() and other sanitization methods before using $_REQUEST/$_GET/$_POST values in database calls.

First, you are setting $_GET['filterN'] but you are trying to use $_GET['condition_N']. That is not ever going to work right.

Secondly, pagename is an "exact match" value, not a search parameter, and it only works with the "Page" post type (at least so far as I can tell). You are using a CPT and, presumably, you do not want an exact match. If you did the other query conditions would pointless. So that isn't going to work correctly either.

The simplest thing you can do is the use the s parameter.

$title = $_GET['s'];
$args = array(
    's' => $title,
    'numberposts' => -1,
    'post_type' => 'my_custom_post',
    'meta_query' => array(
        array(
            'key' => 'filter_1',
            'value' => $_GET['filter1']
        ),
        array(
            'key' => 'filter_2',
            'value' => $_GET['filter2']
        ),
        array(
            'key' => 'filter_3',
            'value' => $_GET['filter3']
        )
    )
);
$the_query = new WP_Query($args);

That will search the post title for your $title string. It will also search post_content. To only search the title you will need to filter the search part of the query.

function only_title_search_wpse_119422($search) {
    remove_filter('posts_search','only_title_search_wpse_119422');
    global $wpdb;
    $pattern = "/\({$wpdb->posts}.post_title[^)]+\)/";
    preg_match_all($pattern,$search,$matches);
    if (!empty($matches[0])) {
      $search = ' AND ('.implode(' AND ',$matches[0]).')';
    }
    return $search;
}
add_filter('posts_search','only_title_search_wpse_119422');

Add that immediately before your secondary query runs.

I can't tell for sure, but I wonder if you shouldn't be using a filter on pre_get_posts instead of the secondary query. The same basic ideas should apply in either case.

First, you are setting $_GET['filterN'] but you are trying to use $_GET['condition_N']. That is not ever going to work right.

Secondly, pagename is an "exact match" value, not a search parameter, and it only works with the "Page" post type (at least so far as I can tell). You are using a CPT and, presumably, you do not want an exact match. If you did the other query conditions would pointless. So that isn't going to work correctly either.

The simplest thing you can do is the use the s parameter.

$title = esc_textarea( $_GET['s'] );
$args = array(
    's'           => $title,
    'numberposts' => -1,
    'post_type'   => 'my_custom_post',
    'meta_query'  => array(
        array(
            'key'   => 'filter_1',
            'value' => esc_attr( $_GET['filter1'] )
        ),
        array(
            'key'   => 'filter_2',
            'value' => esc_attr( $_GET['filter2'] )
        ),
        array(
            'key'   => 'filter_3',
            'value' => esc_attr( $_GET['filter3'] )
        )
    )
);
$the_query = new WP_Query( $args );

That will search the post title for your $title string. It will also search post_content. To only search the title you will need to filter the search part of the query.

function only_title_search_wpse_119422( $search ) {
    remove_filter( 'posts_search', 'only_title_search_wpse_119422' ); 

    global $wpdb; 

    $pattern = "/\({$wpdb->posts}.post_title[^)]+\)/";
    preg_match_all( $pattern, $search, $matches ); 

    if ( ! empty( $matches[0] ) ) {
        $search = sprintf(
            " AND (%s)",
            implode( " AND ", $matches[0] )
        );
    } 

    return $search;
}
add_filter( 'posts_search', 'only_title_search_wpse_119422' );

Add that immediately before your secondary query runs.

I can't tell for sure, but I wonder if you shouldn't be using a filter on pre_get_posts instead of the secondary query. The same basic ideas should apply in either case.

Note: Always use the esc_*() and other sanitization methods before using $_REQUEST/$_GET/$_POST values in database calls.

Source Link
s_ha_dum
  • 65.5k
  • 13
  • 84
  • 174

First, you are setting $_GET['filterN'] but you are trying to use $_GET['condition_N']. That is not ever going to work right.

Secondly, pagename is an "exact match" value, not a search parameter, and it only works with the "Page" post type (at least so far as I can tell). You are using a CPT and, presumably, you do not want an exact match. If you did the other query conditions would pointless. So that isn't going to work correctly either.

The simplest thing you can do is the use the s parameter.

$title = $_GET['s'];
$args = array(
    's' => $title,
    'numberposts' => -1,
    'post_type' => 'my_custom_post',
    'meta_query' => array(
        array(
            'key' => 'filter_1',
            'value' => $_GET['filter1']
        ),
        array(
            'key' => 'filter_2',
            'value' => $_GET['filter2']
        ),
        array(
            'key' => 'filter_3',
            'value' => $_GET['filter3']
        )
    )
);
$the_query = new WP_Query($args);

That will search the post title for your $title string. It will also search post_content. To only search the title you will need to filter the search part of the query.

function only_title_search_wpse_119422($search) {
    remove_filter('posts_search','only_title_search_wpse_119422');
    global $wpdb;
    $pattern = "/\({$wpdb->posts}.post_title[^)]+\)/";
    preg_match_all($pattern,$search,$matches);
    if (!empty($matches[0])) {
      $search = ' AND ('.implode(' AND ',$matches[0]).')';
    }
    return $search;
}
add_filter('posts_search','only_title_search_wpse_119422');

Add that immediately before your secondary query runs.

I can't tell for sure, but I wonder if you shouldn't be using a filter on pre_get_posts instead of the secondary query. The same basic ideas should apply in either case.