Domanda

E 'possibile utilizzare WP_Query per restituire un elenco filtrato di elementi sulla base dei seguenti criteri set? Mi sembra di essere in lotta in quanto vi sono numerosi quesiti contro i campi personalizzati.

Select all posts that are of type business_club (post_type)
Where the post has a zone of 'Asia' (meta_value)
Order by country ASC (meta_value)
then by town ASC (meta_value)
then by title (wp value)

Ogni aiuto è molto apprezzato.

È stato utile?

Soluzione 3

Il modo intorno ad esso era quello di costruire una query personalizzata. Ulteriori informazioni su questo possono essere visualizzate sul Codex Wordpress a http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query .

Il mio codice finale sembrava come segue

<?php               
$row = 0;
$zone = $_GET['zone'];
if (!$zone) $zone = "United Kingdom";
?>

<table class="formattedTable" cellspacing="1" cellpadding="0">
<tr>
    <th width="140">Country</th>
    <th width="140">Town</th>
    <th>Club</th>
</tr>
<?php

$query = "
SELECT
  posts.*
FROM
  $wpdb->posts posts
INNER JOIN
  $wpdb->postmeta meta1 ON posts.ID = meta1.post_ID
INNER JOIN
  $wpdb->postmeta meta2 ON posts.ID = meta2.post_ID
INNER JOIN
  $wpdb->postmeta meta3 ON posts.ID = meta3.post_ID
WHERE
  posts.post_type = 'club' AND
  posts.post_status = 'publish' AND
  meta1.meta_key = '_club-zone' AND
  meta1.meta_value = '$zone' AND
  meta2.meta_key = '_club-country' AND
  meta3.meta_key = '_club-town'
ORDER BY
  meta2.meta_value,
  meta3.meta_value,
  posts.post_title";

$posts = $wpdb->get_results($query, object);        
if ($posts)
    foreach($posts as $post)
    {
        global $post;
        setup_postdata($post);

        echo '<tr class="' . ($row % 2 == 0 ? 'odd' : 'even') . '">';
        echo '<td>' . get_post_meta(get_the_ID(), '_club-country', true) . '</td>';
        echo '<td>' . get_post_meta(get_the_ID(), '_club-town', true) . '</td>';
        echo '<td><a href="' . get_permalink(get_the_ID()) . '">' . get_the_title() . '</a></td>';
        echo '</tr>';
        $row++;
    }

?>
</table>

Altri suggerimenti

Hai provato qualcosa di simile:

$loop = new WP_Query( array( 
    'post_type' => 'business_club',
    'meta_value' => 'United Kingdom',
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => 'zone'
 ) ); 
while ( $loop->have_posts() ) : $loop->the_post();

Non so ancora se si può fare il quindi orderby città e titolo .

Se tutti i risultati hanno la stessa zona, perché si vuole ordine a seconda della zona?

Per quanto ne so, non è possibile includere due meta_keys in una singola query. Quindi dovrete scrivere la query per restituire i risultati in ordine di titolo, salvarle in un array, e quindi ricorrere l'array nell'ordine avete bisogno utilizzando PHP.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top