1 Answer
  • 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 190

Backtrace:

File: /var/www/html/thecodehelp.com/application/views/question.php
Line: 190
Function: _error_handler

File: /var/www/html/thecodehelp.com/application/controllers/Questions.php
Line: 416
Function: view

File: /var/www/html/thecodehelp.com/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Using WP_Query with custom SQL?

I want to use this SQL.

"ORDER BY CASE WHEN post_title >= 'C' THEN 1 ELSE 0 END DESC, post_title ASC".

This SQL sort posts and returns posts that starts with letter "C". I wanna to do search posts with sorting by first letter. This SQL code is my decision.

  • I try add this part of SQL with filter "posts_orderby". But this not work.
  • I try $wpq = new WP_Query($query);
$sql = "SELECT post_title FROM `baddoc_posts` WHERE post_title LIKE '%Иван%' AND post_type = 'doctor' ORDER BY CASE WHEN post_title >= 'И' THEN 1 ELSE 0 END DESC, post_title ASC";
$wpq = new WP_Query();
$wpq->parse_query($sql);
$wpq->get_posts();
function wpse_298888_posts_where( $where, $query ) {
    global $wpdb;

    $starts_with = $query->get( 'starts_with' );

    if ( $starts_with ) {
        $where .= " ORDER BY CASE WHEN $wpdb->posts.post_title >= '$starts_with' THEN 1 ELSE 0 END DESC, $wpdb->posts.post_title ASC";
    }

    return $where;
}
add_filter( 'posts_orderby', 'wpse_298888_posts_where', 10, 2 );
function wpse_postorderby( $orderby, $query ) {
global $wpdb;

$starts_with = $query->get( 'starts_with' );

if ( $starts_with ) {
    $orderby = "CASE WHEN post_title >= '$starts_with' THEN 1 ELSE 0 END DESC, post_title ASC";
}

return $orderby;

} add_filter( 'posts_orderby', 'wpse_postorderby', 10, 2 );

I solve! Just delete '.' before =

  • 0
Reply Report