• Resolved GarrettB

    (@garrettb)


    Hello

    Could you give me an example of what text/ to put in the “Search parameter’ entry in “Query Post” section if I wanted to ,say:

    Only include pages whose title/name begins with “S”

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @garrettb,
    By default, this feature isn’t available. But you can achieve this with a custom filter.

    Copy the code below and paste it to your Theme Functions file.
    Note: You have to replace the word How with the text you want to use for the search query.

    Code:

    function wpse_298888_posts_where( $where, $query ) {
        global $wpdb;
    
        $starts_with = esc_sql( $query->get( 'starts_with' ) );
    
        if ( $starts_with ) {
            $where .= " AND $wpdb->posts.post_title LIKE '$starts_with%'";
        }
    
        return $where;
    }
    add_filter( 'posts_where', 'wpse_298888_posts_where', 10, 2 );
    
    function post_grid_query_args_20200429($query_args, $args){
        
      
      $extra_query = array (
    
          'starts_with' => 'How',
    
          );
    
      return array_merge($query_args, $extra_query);
      }
    
    add_filter('post_grid_query_args','post_grid_query_args_20200429', 10, 2);

    Where to Paste?
    From your WordPress dashboard, go to Appearance => Theme File Editor. Select the Theme Functions file from the right sidebar and paste the code to the bottom of that file.
    Screenshot: https://i.imgur.com/hNBKNyU.png

    Let us know if it helps.
    Regards.

    Thread Starter GarrettB

    (@garrettb)

    Thanks Raju

    It’s ok – I will find a different way to do this.
    I have been down the road of WordPress development, and this time, I just want to ‘play’ with WordPress – no code, just plugins and settings.
    Thanks
    Garrett

    Best wishes.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Query Post – Search Parameter example’ is closed to new replies.