Skip to main content
Bumped by Community user
Fixed code
Source Link
Max
  • 53
  • 6

(check the edit below) Is it possible to sort the post based on a custom field?

I have a custom field called "value_date" where I enteradd a date e e.g yyyy/mm/dd

I used a template for custom post taxonomy-event-date.php and this is code in the function, It doesn't work.

<!-- // The Query // -->
<?php
if( have_posts() ) {
$alt_post = 'alt-post';
while( have_posts() ) {
the_post();
$alt_post = $alt_post == 'alt-post' ? '' : 'alt-post';
?>
<div class="box-page-shows-block">
        <?php
        global $wp_query;
        $postid = $wp_query->post->ID;
        echo get_post_meta($postid, 'value_date', true);
        wp_reset_query();
        ?>
        </div>
        <h2><?php the_title(); ?></h2>
        <?php
        }
        }
        ?>

EDITFunctions.php Not working only displayed one post Loop in template for custom post taxonomy-event-date.php

      <?php
       $args = arrayadd_action(
      'post_status'       => 'publish',
      'posts_per_page'    => 6,
      'paged'             => $paged,
      'meta_key'          => 'value_date',
      'orderby'           => 'meta_value_num''pre_get_posts',
      'order'             => 'ASC'
    );
    $query = new WP_Query( $args 'search_filter');
    iffunction search_filter( $query->have_posts() ) {
        whileif ( $query->have_posts!is_admin() ) {
           && $query->the_post();?> 
    <h2><?php the_titleis_tax(); ?></h2>
    <?php                        
     }
 'event', 'date' ) ) }{
     wp_reset_query();  // Restore global post data$today stomped= the_postdate().
   'Y-m-d' ?>

Functions.php

add_action('pre_get_posts','search_filter');
function search_filter($query){
    if ( !is_admin() && $query->is_tax>set( 'event''post_status', 'date' ) 'publish') {;
        $query->set('post_status''meta_value', 'publish'$today);
        $query->set('meta_key', 'value_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}

(check the edit below) Is it possible to sort the post based on a custom field?

I have a custom field called "value_date" where I enter a date e.g yyyy/mm/dd

I used a template for custom post taxonomy-event-date.php

<!-- // The Query // -->
<?php
if( have_posts() ) {
$alt_post = 'alt-post';
while( have_posts() ) {
the_post();
$alt_post = $alt_post == 'alt-post' ? '' : 'alt-post';
?>
<div class="box-page-shows-block">
        <?php
        global $wp_query;
        $postid = $wp_query->post->ID;
        echo get_post_meta($postid, 'value_date', true);
        wp_reset_query();
        ?>
        </div>
        <h2><?php the_title(); ?></h2>
        <?php
        }
        }
        ?>

EDIT Not working only displayed one post Loop in template for custom post taxonomy-event-date.php

      <?php
       $args = array(
      'post_status'       => 'publish',
      'posts_per_page'    => 6,
      'paged'             => $paged,
      'meta_key'          => 'value_date',
      'orderby'           => 'meta_value_num',
      'order'             => 'ASC'
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();?> 
    <h2><?php the_title(); ?></h2>
    <?php                        
     }
     }
     wp_reset_query();  // Restore global post data stomped the_post().
    ?>

Functions.php

add_action('pre_get_posts','search_filter');
function search_filter($query){
    if ( !is_admin() && $query->is_tax( 'event', 'date' ) ) {
        $query->set('post_status', 'publish');
        $query->set('meta_key', 'value_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}

I have a custom field called "value_date" where add a date e.g yyyy/mm/dd

I used a template for custom post taxonomy-event-date.php and this is code in the function, It doesn't work.

Functions.php

add_action('pre_get_posts','search_filter');
function search_filter($query){
    if ( !is_admin() && $query->
is_tax( 'event', 'date' ) ) {
        $today = date( 'Y-m-d' );
        $query->set('post_status', 'publish');
        $query->set('meta_value', $today);
        $query->set('meta_key', 'value_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}
added 14 characters in body
Source Link
Max
  • 53
  • 6
            <?php
       $args = array(
      'post_status'       => 'publish',
      'posts_per_page'    => 6,
      'paged'             => $paged,
      'meta_key'          => 'value_date',
      'orderby'           => 'meta_value_num',
      'order'             => 'ASC'
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();?> 
    <h2><?php the_title(); ?></h2>
    <?php
                          
   
   }
     }
     wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
            <?php
    $args = array(
      'post_status'       => 'publish',
      'posts_per_page'    => 6,
      'paged'             => $paged,
      'meta_key'          => 'value_date',
      'orderby'           => 'meta_value_num',
      'order'             => 'ASC'
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();?> 
    <h2><?php the_title(); ?></h2>
<?php
                           
 }
 }
wp_reset_query();  // Restore global post data stomped by the_post().
?>
      <?php
       $args = array(
      'post_status'       => 'publish',
      'posts_per_page'    => 6,
      'paged'             => $paged,
      'meta_key'          => 'value_date',
      'orderby'           => 'meta_value_num',
      'order'             => 'ASC'
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();?> 
    <h2><?php the_title(); ?></h2>
    <?php                         
     }
     }
     wp_reset_query();  // Restore global post data stomped the_post().
    ?>
added 24 characters in body
Source Link
Max
  • 53
  • 6

Is(check the edit below) Is it possible to sort the post based on a custom field?

Is it possible to sort the post based on a custom field?

(check the edit below) Is it possible to sort the post based on a custom field?

added 10 characters in body
Source Link
Max
  • 53
  • 6
Loading
edited body
Source Link
Max
  • 53
  • 6
Loading
added 1157 characters in body
Source Link
Max
  • 53
  • 6
Loading
added 15 characters in body
Source Link
Max
  • 53
  • 6
Loading
added 61 characters in body
Source Link
Max
  • 53
  • 6
Loading
added 359 characters in body
Source Link
Max
  • 53
  • 6
Loading
deleted 2 characters in body
Source Link
Max
  • 53
  • 6
Loading
Fixed Markdown, removed clutter and unnecessary tags
Source Link
Max Yudin
  • 6.4k
  • 2
  • 25
  • 36
Loading
Source Link
Max
  • 53
  • 6
Loading