Support

Account

Home Forums General Issues Query custom post type e custom field

Solved

Query custom post type e custom field

  • Hi guys, I’ve a custom post type called issue_number with some custom fields including a field called year.
    ok.

    I want realize a query that it does this:
    – a list of YEAR with every issue_number of that year.
    YEAR X–>issue_number 1 and thumbnail – issue_number 2 and thumbnail – issue_number 3 and thumbnail

    YEAR Y–>issue_number 4 and thumbnail – issue_number 5 and thumbnail -issue_number 6 and thumbnail

    etc.

    My code for moment is this :

    `<?php

    // args
    $args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘issue_number’,
    ‘posts_per_page’ => 100,
    );

    // query
    $the_query = new WP_Query( $args );

    ?>
    <?php if( $the_query->have_posts() ): ?>

    <?php endif; ?>

    <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

    But work in half because the result is :

    YEAR X –> issue_number 1 and thumbnail
    YEAR X –> issue_number 2 and thumbnail
    Etc

    thanks!

  • I resolved the problem adding a new custom post type called ANNO and 1 relationship field called anno_issue from year to issue_number.
    The code is this for query :

    ` <?php

    // args
    $args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘anno’,
    ‘posts_per_page’ => 100,
    );

    // query
    $the_query = new WP_Query( $args );

    ?>

    <?php while( $the_query->have_posts() ) : $the_query->the_post();
    mh_before_page_content();
    mh_magazine_page_title();?>

    <?php

    /*
    * Query posts for a relationship value.
    * This method uses the meta_query LIKE to match the string “123” to the database value a:1:{i:0;s:3:”123″;} (serialized array)
    */

    $posts = get_posts(array(
    ‘post_type’ => ‘issue_number’, //nome post type da dove recupero le info
    ‘posts_per_page’ => -1,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘anno_issue’, // nome custom field all’interno di post che mi da l’info
    ‘value’ => ‘”‘ . get_the_ID() . ‘”‘, // matches exaclty “123”, not just 123. This prevents a match for “1234”
    ‘compare’ => ‘LIKE’
    )
    )
    ));

    ?>
    <?php /*ciclo stampa articoli */ ?>
    <?php if( $posts ): ?>

    <?php foreach( $posts as $post ): ?>
    <?php /*layout 1 del tema per articoli */ ?>
    <article >

    —HTML CODE DISPLAYNG YOUR LOOP—
    </article>
    <?php /*fine layout 1 */ ?>
    <?php endforeach; ?>

    <?php endif; ?>

    <?php /*fine ciclo stampa articoli */ ?>

    <?php endwhile; // end of the loop. ?>

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query custom post type e custom field’ is closed to new replies.