Support

Account

Home Forums General Issues Query custom post type e custom field Reply To: Query custom post type e custom field

  • 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. ?>