Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Only Display posts with specific custom field value within string or array

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #145580
    Esquivelia
    Participant

    Ok so I have everything displaying correctly but if there are terms other than the conditional term ‘Non-Marine’ within the field ‘value’ for the custom field “item_tags” it does not display those.

    Basically I’m looking for posts with :
    1. Custom post_type – ait-dir-item
    2. Custom field location – annapolis
    3. Custom field item_tags – Non-Marine (this value is within other terms seperated by commas)

    I’m also not sure if these values are strings or arrays?

    Here is the code I have so far:

    <?php
    $args = array( 'post_type' => 'ait-dir-item', 
                   'meta_query' => array(
                        array(
                            'key' => 'location',
                            'value' => 'annapolis'
                        ),
                        array(
                            'key' => 'item_tags',
                            'value' => 'non-marine'
                        )
                    ),
                    'orderby' => 'title', 
                    'order' => 'ASC',
                   'posts_per_page' => 300 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        the_title('<h3 class="entry-title">', '</h3>');
        echo '<div class="entry-content">';
        the_content();
        echo '</div>';
    endwhile;
    ?>
    

    Thanks for any input!

    Cory-

    #145621
    Senff
    Participant

    I think you’re doing it right so I’m not sure why it isn’t working.

    You might want to add a “compare” in the meta_query array, who knows that might make a difference. See http://codex.wordpress.org/Class_Reference/WP_Meta_Query for details.

    #145643
    Esquivelia
    Participant

    Senff,

    Thanks man! Working now!

        <?php
            $args = array( 'post_type' => 'ait-dir-item', 
                           'meta_query' => array(
                                array(
                                    'key' => 'location',
                                    'value' => 'annapolis'
                                ),
                                array(
                                    'key' => 'item_tags',
                                    'value' => 'Non-Marine',
                                    'compare' => 'LIKE',
                                )
                            ),
                            'orderby' => 'title', 
                            'order' => 'ASC',
                           'posts_per_page' => 300 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                the_title('<h3 class="entry-title">', '</h3>');
                echo '<div class="entry-content">';
                the_content();
                echo '</div>';
            endwhile;
        ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.