query_post par titre?
5 réponses
- votes
-
- 2011-07-19
afaitfonctionner cela avec l'aide de cepost à lafin.Salut lesgars;
$finalArgs = array ( 'posts_per_page'=>5, 'order' => 'ASC', 'post_type' => 'school' ); // Create a new instance $searchSchools = new WP_Query( $finalArgs ); $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' "); $args = array( 'post__in'=> $mypostids, 'post_type'=>'school', 'orderby'=>'title', 'order'=>'asc' ); $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;
got this working with the help from this post in the end. Cheers guys;
$finalArgs = array ( 'posts_per_page'=>5, 'order' => 'ASC', 'post_type' => 'school' ); // Create a new instance $searchSchools = new WP_Query( $finalArgs ); $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' "); $args = array( 'post__in'=> $mypostids, 'post_type'=>'school', 'orderby'=>'title', 'order'=>'asc' ); $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;
-
- 2011-07-14
<?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; } ?>
$args = array( 'post_title_like' => $str ); $res = new WP_Query($args);
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; } ?>
Then:
$args = array( 'post_title_like' => $str ); $res = new WP_Query($args);
-
Fonctionnetrèsbien saufpour le deuxième argument (le `$ wp_query` référencé),quine semblepasfairepartie du rappel defiltre (voir http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where)et générera unErreur.Works great except for the second argument (the referenced `$wp_query`), which doesn't seem to be part of the filter callback (see http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where) and will generate an error.
- 0
- 2013-10-14
- maryisdead
-
enfait,le codeest correcttel quel,aumoins dans WP 4.1.1.C'est le codex qui omet le deuxième argument,donc si vous déclarez 2 arguments dans `add_filter` comme dans l'exemple,celafonctionnebien.Un autre avantage de cette solutionest qu'ellefonctionnepour lestypes depublicationpersonnalisés.actually, the code is correct as is, at least in WP 4.1.1. It's the codex that's omitting the second argument, so if you declare 2 arguments in `add_filter` as in the example, it works fine. Another advantage of this solution is that it works for custom post types.
- 1
- 2015-03-22
- yitwail
-
Cela devrait être une réponse acceptée car celamontre comment lefaireen utilisant lesfonctionsintégrées de WordPresset sans utiliser de requête SQLpersonnalisée.This should be accepted answer since this shows how to do it using WordPress built-in functions and not using custom SQL query.
- 1
- 2015-04-02
- Sudar
-
il semble que lepremier «%» soitmanquéici.Juste après `` LIKE \ ''.Je l'ai ajoutéet il a commencé àfonctionner (4.2.4)it seems, that first `%` is missed here. Right after `LIKE \'`. I added it and it started working (4.2.4)
- 1
- 2015-08-16
- vladkras
-
Oui,ilmanque lepremier `%`,ajouté aussi,et fonctionne :) (devraitpeut-êtremodifier la réponse?)Yes, missing the first `%` , added too, and works :) ( maybe should edit the answer ? )
- 0
- 2016-09-21
- Toni Michel Caubet
-
Çamarchebien pourmoi,seulementj'ai supprimé `&` sur `functiontitle_like_posts_where ($ where,& $ wp_query) {`,avant `$ wp_query`.Merci à @Brady.Work fine for me, only I removed `&` on `function title_like_posts_where( $where, &$wp_query ) {` , before `$wp_query`. Thanks to @Brady.
- 0
- 2017-01-25
- Jose Carlos Ramos Carmenates
-
- 2011-11-22
Récupérez letitre d'une autreboucle
$title = get_the_title();
et utilisez la variable $title si vous le souhaitez.
<?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 here */ endwhile; endif; wp_reset_query(); ?>
Get the title from another loop
$title = get_the_title();
and use the $title variable if you wish.
<?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 here */ endwhile; endif; wp_reset_query(); ?>
-
celafonctionne sur letype demessagepersonnaliséet wordpress 3.2.1it works on custom post type and wordpress 3.2.1
- 0
- 2011-11-22
- Zakir Sajib
-
- 2011-07-14
Oui,c'estpossible ....
global $wpdb; $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$str%' "); $args = array('post__in'=$mypostids); $res = WP_Query($arg);
Yes it is possible....
global $wpdb; $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title like '%$str%' "); $args = array('post__in'=$mypostids); $res = WP_Query($arg);
-
bravo Rajeev - aessayé cecimaisil restebloqué après leget_col.J'aimis àjour ce quiprécède ^^^cheers Rajeev - tried this but it gets stuck after the get_col. I've updated the above^^^
- 0
- 2011-07-14
- v3nt
-
- 2018-11-16
Ces réponsesme semblent être destentatives depiratage de wordpress.
Reportez-vous à lamême question sur le débordement depile:
https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category
Celafonctionne si vous souhaitezeffectuer une recherchepartitretriéepartitre:
$the_query = new WP_Query( array( 'post_type' => 'watches', 'posts_per_page' => 5, 'orderby' => 'title', 's' => 'my title' ) );
Cetexemple de requête concerne untype depublicationnommémontreset le «s» (terme de recherche)est l'endroit où vouspouvez rechercher lestitres de vosmessages sur la requête
These replies seem to me as trying to hack wordpress.
Refer to the same question on stack overflow:
https://stackoverflow.com/questions/25761593/wp-query-with-post-title-like-something-and-category
This works if you want to do a search query by title ordered by title:
$the_query = new WP_Query( array( 'post_type' => 'watches', 'posts_per_page' => 5, 'orderby' => 'title', 's' => 'my title' ) );
This example query is for a post type named watches and the 's' (search term) is where you can look for your post titles on the query
-
Cela recherchera également le contenuThis will search the content as well
- 1
- 2019-07-02
- Mark Kaplun
Est-ilpossible de créer uneboucle d'articlesen utilisant WP_Query ou query_postsen utilisant letitre?
ie