query_post per titolo?

14 lug 2011, 17:16:48
Visualizzazioni: 88.5K
Voti: 22

È possibile creare un loop di post usando WP_Query o query_posts filtrando per titolo?

ad esempio

// Definizione degli argomenti per la ricerca per titolo
$args = array('post_title'='LIKE '.$str.'% ');

$res = WP_Query($arg);

// il loop...


// Sto provando questo adesso...
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like 'Abb%' ");

echo count($mypostids).", ";    // funziona ma non riesco a mostrare l'array di ID per i successivi args?

$args = array(
    'post__in'=> $mypostids
);

$res = WP_Query($args);

while( $res->have_posts() ) : $res->the_post(); ...
0
Tutte le risposte alla domanda 5
6
37

functions.php

<?php
add_filter( 'posts_where', 'title_like_posts_where', 10, 2 );
function title_like_posts_where( $where, $wp_query ) {
    global $wpdb;
    if ( $post_title_like = $wp_query->get( 'post_title_like' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $post_title_like ) ) . '%\'';
    }
    return $where;
}
?>

Poi:

$args = array(
    'post_title_like' => $str
);
$res = new WP_Query($args);
14 lug 2011 18:21:09
Commenti

Funziona benissimo tranne che per il secondo argomento (il $wp_query referenziato), che non sembra far parte del callback del filtro (vedi http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where) e genererà un errore.

maryisdead maryisdead
14 ott 2013 15:37:09

in realtà, il codice è corretto così com'è, almeno in WP 4.1.1. È il codex che omette il secondo argomento, quindi se dichiari 2 argomenti in add_filter come nell'esempio, funziona perfettamente. Un altro vantaggio di questa soluzione è che funziona per i custom post type.

yitwail yitwail
22 mar 2015 06:49:55

Questa dovrebbe essere la risposta accettata poiché mostra come farlo utilizzando le funzioni integrate di WordPress e non usando query SQL personalizzate.

Sudar Sudar
2 apr 2015 09:11:17

sembra che il primo % manchi qui. Subito dopo LIKE \'. L'ho aggiunto e ha iniziato a funzionare (4.2.4)

vladkras vladkras
16 ago 2015 13:45:14

Sì, manca il primo %, l'ho aggiunto anch'io e funziona :) (forse bisognerebbe modificare la risposta?)

Toni Michel Caubet Toni Michel Caubet
21 set 2016 11:40:03

Funziona bene per me, ho solo rimosso & su function title_like_posts_where( $where, &$wp_query ) {, prima di $wp_query. Grazie a @Brady.

Jose Carlos Ramos Carmenates Jose Carlos Ramos Carmenates
25 gen 2017 18:54:12
Mostra i restanti 1 commenti
0

alla fine ho risolto con l'aiuto di questo post. Grazie ragazzi;

$finalArgs =  array (       
        'posts_per_page'=>5, // Numero di post per pagina
        'order' => 'ASC',    // Ordine crescente
        'post_type' => 'school' // Tipo di post                         
    );

    // Crea una nuova istanza
    $searchSchools = new WP_Query( $finalArgs );

    $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");

    $args = array(
        'post__in'=> $mypostids, // ID dei post da includere
        'post_type'=>'school',   // Tipo di post
        'orderby'=>'title',     // Ordina per titolo
        'order'=>'asc'           // Ordine crescente
    );

    $res = new WP_Query($args);

    while( $res->have_posts() ) : $res->the_post();

        global $post;

        $EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);

        $schl = array('id'=>$EstablishmentNumber, 'label'=>$post->post_title , 'value'=>$EstablishmentNumber );     
        $matchedSchools[] = $schl;


    endwhile;
19 lug 2011 16:04:36
1

Ottieni il titolo da un altro loop

$title = get_the_title();

e utilizza la variabile $title se lo desideri.

<?php

global $post, $current_post_id, $title;

function filter_where($where = ''){

    global $title;
    $where .= "AND post_title = '$title'";
    return $where;

}
add_filter('posts_where', 'filter_where');

$query = new WP_Query(array('post_type' => 'sessions') );
if ( have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

    /* Loop qui */

endwhile; endif; 

wp_reset_query(); ?>
23 nov 2011 01:32:33
Commenti

funziona con i custom post type e WordPress 3.2.1

Zakir Sajib Zakir Sajib
23 nov 2011 01:35:41
2

Sì, è possibile...

global $wpdb;

// Recupera gli ID dei post che contengono la stringa cercata nel titolo
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$str%' ");

// Crea l'array di argomenti per la query
$args = array('post__in' => $mypostids);

// Esegue la query
$res = WP_Query($arg);
14 lug 2011 17:29:47
Commenti

grazie Rajeev - ho provato ma si blocca dopo il get_col. Ho aggiornato sopra^^^

v3nt v3nt
14 lug 2011 18:17:31

Ha funzionato perfettamente, grazie.

csaborio csaborio
11 gen 2021 03:55:30
1

Queste risposte mi sembrano tentativi di hackerare WordPress.

Fai riferimento alla stessa domanda su Stack Overflow:

https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category

Questo funziona se vuoi eseguire una query di ricerca per titolo ordinata per titolo:

$the_query = new WP_Query( 
  array(
    'post_type' => 'watches',
    'posts_per_page' => 5,
    'orderby' => 'title',
    's' => 'my title'
  ) 
);

Questa query di esempio è per un post type chiamato "watches" e il parametro 's' (termine di ricerca) è dove puoi cercare i titoli dei post nella query.

16 nov 2018 13:44:07
Commenti

Questo cercherà anche nel contenuto

Mark Kaplun Mark Kaplun
2 lug 2019 15:17:25