0

I get all post successfully of CPT which are Order by meta_key value Date. Code is below

$today = date("Y/m/j");
query_posts(array(
'post_type' => 'event',
'posts_per_page' => 10,
'meta_key' => '_start_eventtimestamp',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
    array(
       'key' => '_start_eventtimestamp',
       'meta-value' => $value,
       'value' => $today,
       'compare' => '>=',
       'type' => 'CHAR'
   )
)
));
if (have_posts()) :
while (have_posts()) : the_post();

Now trying to display one category at a time using order by meta box value.

post_type=>event, taxonomy=>event_catgory, slug=>green

How can i show these post on index.php and also order by meta_key value which is Date?

  • 1
    You'll need to write a manual loop for that. The posts property of WP_Query is an array of the posts queried. – jdm2112 Feb 24 '18 at 4:01
2

Never use query_posts! Use get_posts() or new WP_Query() instead.

I think this will work however:

$today = date("Y/m/j");
$args = (array(
    'post_type' => 'event',
    'posts_per_page' => 10,
    'orderby' => array(
        'wpse_meta_query_name' => 'ASC',
        'post_title' => 'DESC' // optional second orderby paramater 
    ),    
    'meta_query' => array(
        'wpse_meta_query_name' => array( // give the meta query arguments a name, to pass into 'orderby'
            'key' => '_start_eventtimestamp',
            // 'meta-value' => $value, // this was in your code but should not be
            'value' => $today,
            'compare' => '>=',
            'type' => 'CHAR'
        )
    ),
    'tax_query' => array(
        'relation' => 'AND', // redundant default value 'AND' but I still put it in..
        array(
            'taxonomy' => 'event_catgory',
            'field'    => 'slug',
            'terms'    => 'green',
        ),
    )
));

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post(); // edit: added $query
// ...
    }
}

---- edit ----

I wanted to say a few more things in addition to addressing your comment below about taxonomy-event_category.php

There was on little detail of the question that I decided to overlook, and that's the fact that you are running this query on index.php

When you are on that file, you can be fairly certain that a query has already been executed. To access that query, you could do

global $wp_query
if ( $wp_query->have_posts() ) { ... etc. etc.

This would be identical to the way I did it above, except that the global $wp_query wouldn't have a taxonomy query built into it, this is because you are on index.php

However, you don't need to use global $wp_query. Instead, when you simply run the functions have_posts() and the_post() instead of $variable_that_is_a_wp_query->have_posts(), it simply references the global $wp_query for you. So there is not usually a reason to reference that global variable.

If you want to modify the query that is already run behind the scenes when you are on index.php, you should look at the 'pre_get_posts' hook. You can accomplish the same sort of things as above, but in a slightly different way.

However, when all you want to do is display posts from a category, you don't normally need to do this. Instead, use taxonomy-event_category.php like you mentioned below, the exact 'tax_query' will already be included for you automatically in the global $wp_query, except now it will automatically change based on the url (ie. yoursite.com/event-category/green, or yoursite.com/event-category/blue).

Another thing, index.php will list your posts yes, but index.php works for other post types as well. Actually this is where I could be a little bit wrong. My understanding was that custom post types would use archive.php (which is quite similar to index.php), whereas index.php is kind of a weird file because in some WordPress installs it is your homepage, while in other WordPress installs your homepage could be front-page.php or template-home.php for example, and index.php is simply the 'archive' page to list all posts that are of the 'post' post type (ie. blog posts). (check this confusing diagram for more information: https://developer.wordpress.org/files/2014/10/wp-hierarchy.png)

So a little confused as to why are showing events on index.php. If this page is in fact your home page, you should call it front-page.php. Then in settings->reading, select an option like.. "front page shows a static page". Create a page called home, then select it under the drop down menu. This way, the global $wp_query would not be grabbing a bunch of blog posts from the database, just so you can write your own new query. Instead, it would be just querying for the single post_id that corresponds to your Home page.

So where to from here. I guess my recommendation is to try creating a file called archive-event.php. Then if you want you can just let index.php and archive.php do whatever they want, and you have control over which post type goes to which archive-{post-type}.php page. This is normally how I do it.

If you use that in addition to taxonomy-event_category.php, then you are almost set. The only thing that remains is to add the 'meta_query' so in order to change the ordering of posts from the default of showing the most recent posts first.

Under this setup, archive-event.php shows all events, and taxonomy-event_category.php shows all events but with a certain taxonomy query already applied. The html of these pages might be more or less identical.

So lets try to "modify the main query" via "pre_get_posts", for all front-end main queries that are specifically ties to "events". Try putting this into your functions.php:

 /**
 * @param $query - passed by reference
 */
function wpse_modify_events_main_query( $query ) {

    // this function is run on every single wp_query which happens in a ton of places
    // so we need to figure out the logic required so that we don't unintentionally modify the wrong queries
    // sometimes there seems to be no clear way on exactly how to do this.. different things work for different purposes

    if ( is_admin() ) {
        return; // in /wp-admin, do not modify
    }

    // queries where we manually write 'new WP_Query()' are not main queries
    if ( ! $query->is_main_query() ) {
        return; // not main query, do not modify
    }

    // there may be better ways to verify post type? but this will work.
    if ( isset( $query->query['post_type'] ) && ( $query->query['post_type'] == 'event' ) ) {

        // is a meta_query is already set for one reason or another, we may accidentally overwrite it.
        // this may or may not be intended, so kind of tough to decide what to do if one is already in place.
        //          if ( $query->get('meta_query') ) {
        //              // return ?? probably not.
        //          }

        $today = date("Y/m/j");

        $query->set('meta_query', array(
            'wpse_meta_query_name' => array(
                'key' => '_start_eventtimestamp',
                'value' => $today,
                'compare' => '>=',
                'type' => 'CHAR'
            )
        ) );

        $query->set('orderby', array(
            'wpse_meta_query_name' => 'ASC',
            'post_title' => 'DESC' // optional second orderby paramater
        ) );

    }

    // the $query variable was passed by reference, meaning we don't need to return the $query.
    // when we do things like $query->set() it will modify the main query.
}
// run each query through our own function before it hits the database
add_action( 'pre_get_posts', 'wpse_modify_events_main_query');

If anybody can let me know if there's something I might have missed in the pre_get_posts call that would be nice. WordPress has a lot of functions like.. is_post_type_archive(), is_tax(), that could be used also, but sometimes they don't seem to do exactly what they say.

To check if your query is being modified, use global $wp_query, then print_r on it, see if it has meta_query arguments attached.

  • No output with this query, In my case category name isgreen, So any thing to change in this query id or else? – FRQ6692 Feb 24 '18 at 12:39
  • added $query->the_post() does that help? also verify taxonomy name and post type. try commenting meta query and first line of or orderby, to see if tax query is working. – Joel M Feb 24 '18 at 13:47
  • Thanks its work... Some more help please... At taxonomy-event_category.php how its auto detect terms=>$name and query post related to category? – FRQ6692 Feb 24 '18 at 16:06
  • Added a pretty big edit above, see that for long answer. The short version is that its complicated.. but it looks to the url, an inserts a tax_query into the main $wp_query, in pretty much the same way we did above. – Joel M Feb 24 '18 at 20:36
  • You add Beautiful explantion with answer thumbs up. This is from your text taxonomy-event_category.php to add the 'meta_query' The only thing that remains is to add the 'meta_query' so in order to change the ordering of posts Please Help in meta_query for taxonomy-event-cat.php. Thanks – FRQ6692 Feb 26 '18 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.