• Resolved frobitz

    (@frobitz)


    Hi,

    I’m trying to set up my front-page.php template so that only content (posts or pages) with a custom field which matches “front_page_order” exists, and further it’s ordered by a a numeric value of the custom field.

    I’ve tried many examples I’ve found as well as the documentation for WP_Query and WP_Meta_Query and nothing works as expected. Too many results are returned so it feels like the query is not working

    Here’s one variation of the many attempts I’ve tried:

    <?php get_header(); ?>
    
        <h1>Home page</h1>
    
        <?php if ( have_posts() ) : ?>
    
        <?php
    
                  $args = array(
                    'post_type' => 'any',
                    'meta_key' => 'front_page_order',
                    'orderby' => 'meta_value_num',
                    'order' => 'ASC'
                  );
    
                  $pages = new WP_Query( $args );
    
                  foreach( $pages as $page ) {
    
        ?>
    
          <article>
            <h1><?php echo $page->ID; ?></h1>
          </article>
    
        <?php      } ?>
    
        <?php endif ?>
    
    <?php get_footer(); ?>

    Any help would be appreciated, this has been stumping me for hours.

Viewing 1 replies (of 1 total)
  • Thread Starter frobitz

    (@frobitz)

    Solve it. My poor understanding of what was going on around the query was to blame. Here’s a solution:

    <?php get_header(); ?>
    
        <h1>Home page</h1>
    
        <?php
    
          $args = array(
            'post_type' => 'any',
            'meta_key' => 'front_page_order',
            'orderby' => 'meta_value_num',
            'order' => 'ASC'
          );
    
          $boxes = new WP_Query( $args );
    
          if ( $boxes->have_posts() ) :
    
            while($boxes->have_posts()) :
    
              $boxes->the_post();
    
        ?>
    
          <article>
            <h1><?php echo the_title() ?></h1>
          </article>
    
        <?php
    
            endwhile;
    
          endif;
    
        ?>
    
    <?php get_footer(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘In front-page.php I want to show only pages/posts with a particular custom field’ is closed to new replies.