Sort by date archive-custotype.php
-
Hi All,
I have created a custom type with a date field (not content creation). The custom type was created for scheduled events so it is key that they are sort by date.
I have the following code in archive.php. Please guide me to develop archive-custotype.php
<?php if ( have_posts() ) : ?> <header class="page-header"> <?php the_archive_title( '<h1 class="page-title">', '</h1>' ); the_archive_description( '<div class="archive-description">', '</div>' ); ?> </header><!-- .page-header --> <?php /* Start the Loop */ while ( have_posts() ) : the_post(); /* * Include the Post-Format-specific template for the content. * If you want to override this in a child theme, then include a file * called content-___.php (where ___ is the Post Format name) and that will be used instead. */ get_template_part( 'template-parts/content', get_post_format() ); endwhile; the_posts_navigation(); else : get_template_part( 'template-parts/content', 'none' ); endif; ?>Thanks in advance
The page I need help with: [log in to see the link]
-
you should use pre_get_posts() to change the sort order when the query to fetch posts for that page is executed.
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
Then you only need to do whatever’s necessary in that template to format stuff the way you want.
Hi @sterndata! Thanks a lor for your feedback.
Sorry but I will need some more tips to develope this. If I well undertood, i have to add this to my code:
<?php add_action( 'pre_get_posts', 'your_function_name' ); ?>Then I have to define ‘your_function_name’ in functions file. Right?
Something like
function sort_by_date(){ }Sorry for the basic question, but long time not developing…
Could you give me some guidelines for the function develop?
Thanks in advance.
-
This reply was modified 7 years, 4 months ago by
Astilla VGR.
You’ll need to re-order the query by your custom field, so you’d need to use a meta-key query as part of the query you’re modifying. This is not a simple cookbook thing.
Look at the documentation for get_posts() and wp_query() for examples of what you’d need to add the the page query via pre_get_posts().
@sterndata I have found this (to be added on functions.php)
function prefix_modify_query_order( $query ) { if ( is_main_query() ) { $args = array( 'post_date' => 'ASC' ); $query->set( 'orderby', $args ); } } add_action( 'pre_get_posts', 'prefix_modify_query_order' );Do you think that it could works changin ‘post_date’ to my custom fild date? I will check the documentation you suggest.
No, because custom fields require a meta query (a query on the metadata).
Is this more accurate example?
<?php function my_pre_get_posts( $query ) { // do not modify queries in the admin if( is_admin() ) { return $query; } // only modify queries for 'event' post type if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) { $query->set('orderby', 'meta_value'); $query->set('meta_key', 'start_date'); $query->set('order', 'DESC'); } // return return $query; } add_action('pre_get_posts', 'my_pre_get_posts'); ?>I think you’re getting there!
I assume these are the variables to be updated:
$query->set('orderby', 'meta_value'); $query->set('meta_key', 'start_date'); $query->set('order', 'DESC');if programada custom post type should be order descent by fecha custom field:
$query->set('orderby', '*****'); $query->set('meta_key', 'fecha'); $query->set('order', 'DESC');What should be the value for meta_value? are the other variables ok?
You should always set ‘orderby’ to either ‘meta_value’ or ‘meta_value_num’ regardless of what meta_key you are ordering by. You actually do not need a meta_value argument if you are also using “meta_query”. If you did supply a meta_value argument, the query will only return posts whose value is equal to the argument, in which case there is little point in ordering by that field since they all have the same value.
You almost certainly need to set a “meta_query” value. In this case, the “meta_key” argument is just to tell WP what key is to be used for ordering by meta_value. The actual query criteria is all specified in “meta_query”. For specifics on using meta_query, see https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
It is really strange, because the following code works
function my_pre_get_posts( $query ) { $query->set('orderby', 'meta_value'); $query->set('order', 'ASC'); // return return $query; }But the folliwn one, not:
function my_pre_get_posts( $query ) { $query->set('orderby', 'meta_value'); $query->set('meta_key', 'fecha'); $query->set('order', 'ASC'); // return return $query; }Why if I introduce
$query->set('meta_key', 'fecha');it does returns and empty query?If you orderby meta_value but do not supply a meta_key argument, the argument is ignored and the default ordering (post_date) is used. You will get all posts, regardless of meta_value, limited only by the posts_per_page value.
If you orderby meta_value and supply a meta_key argument, but do not provide either a meta_value or meta_query argument, you should get all posts that have any sort of ‘fecha’ value assigned. I don’t know why your query is failing. It may conflict with other query vars already set. Perhaps examining the actual SQL generated by WP will tell us why. To see the SQL, add this line on your template somewhere outside of the loop:
<pre><?php echo get_queried_object()->request; ?></pre>While the answer may be informative, the fact remains you need to supply either a meta_value or meta_query argument. I said earlier you should use meta_query, but I had forgotten about the possibility of meta_compare arguments. You should be able do something like
$query->set('orderby', 'meta_value'); $query->set('meta_key', 'fecha'); $query->set('meta_value', '2018-01-15'); $query->set('meta_compare', '>'); $query->set('order', 'ASC');Hi @bcworkz! Im back to this issue.
Here my functions.php as it is right now:
<?php function my_theme_enqueue_styles() { $parent_style = 'foundation-css-framework'; // This is 'foundation-css-framework' child theme. wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' ); wp_enqueue_style( 'clubcotacero', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version') ); } function my_pre_get_posts( $query ) { $query->set('orderby', 'meta_value'); $query->set('meta_key', 'fecha'); $query->set('order', 'ASC'); // return return $query; } /*add_action('pre_get_posts', 'my_pre_get_posts');*/ add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); ?>And here my archive.php file:
<?php /** * The template for displaying archive pages * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/ * * @package Foundation_CSS_framework */ get_header(); ?> <div id="primary" class="content-area large-12 medium-8 small-12 cell grid-x"> <main id="main" class="site-main"> <?php if ( have_posts() ) : ?> <header class="page-header large-12 cell"> <h1 class="page-title"> Activdades Programadas </h1> <?php the_archive_description( '<div class="archive-description">', '</div>' ); ?> </header><!-- .page-header --> <?php /* Start the Loop */ /* Mirando el resultado de la busqueda */ /**/ while ( have_posts() ) : the_post(); /* * Llamamos a nuestro propio content solo para listas */ get_template_part( 'template-parts/content-programada-lista', get_post_format() ); endwhile; the_posts_navigation(); else : get_template_part( 'template-parts/content', 'none' ); endif; ?> </main><!-- #main --> </div><!-- #primary --> <?php get_sidebar(); get_footer();I tryed to paste in the code your line:
<?php echo get_queried_object()->request;But obviously it is not a “copy/paste” solution. I have this:
Notice: Undefined property: WP_Post_Type::$request in C:\xampp\htdocs\testeo\wp-content\themes\clubcotacero\archive-programada.php on line 42So I assume I have to change “->request”. Could you please shed some light on this? It would be really appreciated.
Kind Regards
VíctorR-
This reply was modified 7 years, 4 months ago by
Astilla VGR.
I’m sorry, I don’t why I came up with that code, it doesn’t even make sense. What I should have given you:
<?php global $wp_query; echo $wp_query->request; ?>Thanks! Here the result:
SELECT SQL_CALC_FOUND_ROWS lm_posts.ID FROM lm_posts WHERE 1=1 AND lm_posts.post_type = ‘programada’ AND (lm_posts.post_status = ‘publish’) ORDER BY lm_posts.post_date DESC LIMIT 0, 10It seems that it is not ordering by date custom field instead it is using post_date. No idea where is the issue…any advice?
THE MAIN ISSUE: It is needed to add “WPCF-” before the custom field slug.
-
This reply was modified 7 years, 4 months ago by
The topic ‘Sort by date archive-custotype.php’ is closed to new replies.