how to make sorting by meta_key in wordpress

in my search page wordpress sorting post by date.
But i want sorting to meta_key in my template – imic_date (date of event).
I need to modify the search query, but where?
code that should work. But… where I add it?

$args = array(
    'post_type' => array('custom-post-type'), 
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => 'imic_date'
    );
$loop = new WP_Query($args);

(sorry for my english) 😀

Read More

edit:

my search.php file:

    <?php get_header(); ?>
<div class="container">
    <div class="row">
        <div class="col-md-9 posts-archive">
        <?php
            if (have_posts()) :
                while (have_posts()):the_post();
                    echo'<article class="post">
                            <div class="row">';
                    if (has_post_thumbnail()):
                        echo '<div class="col-md-4 col-sm-4">
                        <a href="' . get_permalink() . '">';
                        the_post_thumbnail('600x400', array('class' => "img-thumbnail"));
                        echo'</a></div>';
                    endif;
                    echo '<div class="col-md-8 col-sm-8">';
                    echo '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';
                    echo '<span class="post-meta meta-data">
                            <span><i class="fa fa-calendar"></i>' . get_the_time(get_option('date_format')) . '</span><br><span>Time:<br/>Date:<br/>Price:<br/>Venue:<br/>Category:</span><span></i><br/>'.imic_custom_taxonomies_terms_links().'</span> <span>';
                    comments_popup_link('<i class="fa fa-comment"></i>'.__('No comments yet','framework'), '<i class="fa fa-comment"></i>1', '<i class="fa fa-comment"></i>%', 'comments-link',__(' ','framework'));
                    echo'</span></span>';
                    //echo imic_excerpt(50);
                    echo '<p><a href="' . get_permalink() . '" class="btn btn-primary">' . __('More ', 'framework') . '<i class="fa fa-long-arrow-right"></i></a></p>';
                    echo '</div></div>';
                    echo '</article>';
                endwhile;
            else:
                echo '<article class="post">';
                if (current_user_can('edit_posts')) :
                    ?>
                    <h3><?php _e('NOT', 'framework'); ?></h3>
                    <p><?php printf(__('NOT', 'framework'), admin_url('post-new.php')); ?></p>
                <?php else : ?>
                    <h3><?php _e('NOT', 'framework'); ?></h3>
                    <p><?php printf(__('NOT', 'framework')); ?></p>
                    <?php
                    echo '</article>';
                endif;
                ?>
            <?php
            endif; // end have_posts() check 
            pagination();
            ?>

        </div>
        <!-- Start Sidebar -->
        <?php 
            echo '<div class="col-md-3 sidebar">';
            dynamic_sidebar('sidebar');
            echo '</div>';
         ?>
        <!-- End Sidebar -->
    </div>
</div>
<?php get_footer(); ?>

Related posts

Leave a Reply

2 comments

  1. Try this code.

    $args = array(
        'post_type' => array('custom-post-type'), 
        'order_by' => array('imic_date'),
        'order' => 'ASC',
    );
    $loop = new WP_Query($args);
    

    edit,

    modify your code like this,

    $args = array(
        'post_type' => array('custom-post-type'), 
        'order_by' => array('imic_date'),
        'order' => 'ASC',
    );
    $loop = new WP_Query($args);
    if ($loop->have_posts()) :
                while ($loop->have_posts()):$loop->the_post();
    /** and then your code as used in original file */
    

    Edit,

    use the code bellow, it works ( I tested )

    <?php $args = array(
    'post_type' => 'event',
    'orderby' => 'meta_value',
    'meta_query' => array(array('key' => 'imic_date')),
    'order' => 'desc',
    'paged'=>$paged,
    );
    $loop = new WP_Query($args);
    if ($loop->have_posts()) :
    while ($loop->have_posts()):$loop->the_post(); ?>
    <?php the_title(); ?>
    

    and your rest of the code here..

  2. global $wpdb;

    $tb_posts = $wpdb->prefix . ‘posts’;

    $tb_postmeta = $wpdb->prefix . ‘postmeta’;

    $query = $wpdb->get_results(“SELECT $colums
    FROM $tb_posts
    INNER JOIN $tb_postmeta
    ON $tb_posts.ID = $tb_postmeta.post_id
    WHERE $tb_postmeta.meta_key IN (‘your_meta_key’)
    AND $tb_posts.post_status = ‘publish’
    ORDER BY post_date DESC
    LIMIT 1

    );

    return $query;