Support

Account

Forum Replies Created

  • function change_order_for_events( $query ) {
        //only show future events and events in the last 24hours
        $yesterday = date('Ymd');
    
        if ( $query->is_main_query() && (is_tax('dt_portfolio_category') || is_post_type_archive('dt_portfolio')) ) {
            $query->set( 'meta_key', 'sub_seminars_0_start_date' );
            $query->set( 'orderby', 'meta_value_num' );
            $query->set( 'order', 'ASC' );
    
            //Get events after 24 hours ago
            $query->set( 'meta_value', $yesterday );
            $query->set( 'meta_compare', '>' );
    
           //Get events before now
           //$query->set( 'meta_value', current_time('timestamp') );
           //$query->set( 'meta_compare', '<' );
        }
    
    }
    
    add_action( 'pre_get_posts', 'change_order_for_events' );

    this code in function.php will sort your posts based on a specific meta key value.
    sub_seminars_0_start_date

    I have created a custom taxonomy Seminar Venues but the above code won’t work for that even after adjustin the parameters.

  • I don’t know what I did but now it seems to work fine.

  • Hi @james,

    I have multiple repeater rows but still when I echo the count () function, it shows me that only 1 row is there.

     <?php $rows = count(get_field('sub_seminars')); 
          echo count($rows); ?>

    I don’t know what is wrong, and this is giving me nightmares.

    Even the repeater that has no rows, it shows count as 1

  • Hi @james

    Thanks for the help, but the problem still remains the same.

    //$row = count( get_field('sub_seminars') );
    //var_dump($row); //this should output the number of rows on returned for you to ensure that the correct value is returned.
    //var_dump($post->ID) //also confirm that the correct value is in use
           //if ($row == 0) {
                $postid = $post->ID; //Supply post-Id here $post->ID.
                wp_update_post(array(
                'ID'    =>  123,
                'post_status'   =>  'draft'
            ));
    //}

    This does set the post with id 123 to draft, so that means I have some problem with the count function.

  • Hi James,

    Could you please give me some more info, perhaps an example please.

    Thanks

  •  $ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
            $startdate = date("Ymd", strtotime($ap));
            $todaydate = date("Ymd");
          if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
           $del_data = array(
                        'Ref' => 'sub_seminars_0_ref',
                        'Start date' => 'sub_seminars_0_start_date',
                        'End Date' => 'sub_seminars_0_end_date',
                        'Venue' => 'sub_seminars_0_venue',
                        'Fees' => 'sub_seminars_0_fees',
                        'CPE Credits' => 'sub_seminars_0_cpe_credits'
            );
          delete_row('sub_seminars', 1);
          
        }
    
        $row = count( get_field('sub_seminars') );
           if ($row == 0) {
                $postid = $post->ID; //Supply post-Id here $post->ID.
                wp_update_post(array(
                'ID'    =>  $postid,
                'post_status'   =>  'draft'
            ));
    
           }

    There seems to be someproblem with my code. Please take a look

  • Could you give a little example how to set the post to private after counting the rows ?

    I mean I don’t know the function to set the post to private ..

  • This functionality is available in search and filter pro (a must have powerful plugin)

  • $ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
            $startdate = date("Ymd", strtotime($ap));
            $todaydate = date("Ymd");
          if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
           $del_data = array(
                        'Ref' => 'sub_seminars_0_ref',
                        'Start date' => 'sub_seminars_0_start_date',
                        'End Date' => 'sub_seminars_0_end_date',
                        'Venue' => 'sub_seminars_0_venue',
                        'Fees' => 'sub_seminars_0_fees',
                        'CPE Credits' => 'sub_seminars_0_cpe_credits'
            );
          delete_row('sub_seminars', 1);
        }

    This worked perfectly

  • Thanks for the reply John but this didn’t work for me. The order remains still the same !!

  • You are right, I do have these fields. However I only want to sort the posts based on the first meta key sub_seminars_0_start_date
    I don’t bother about the other fields. Just this one

  • Someone Please check I have the same problem and I can’t fix it.

    I want to take the different values of sub_field “start_date” and display posts with individual value.

    My code in case it helps `<?php

    /**
    * The main template file.
    *
    * This is the most generic template file in a WordPress theme
    * and one of the two required files for a theme (the other being style.css).
    * It is used to display a page when nothing more specific matches a query.
    * E.g., it puts together the home page when no home.php file exists.
    * Learn more: http://codex.wordpress.org/Template_Hierarchy
    *
    * @package presscore
    * @since presscore 0.1
    */

    // File Security Check

    if ( ! defined( ‘ABSPATH’ ) ) { exit; }

    $config = Presscore_Config::get_instance();
    $config->set( ‘template’, ‘blog’ );
    $config->set( ‘layout’, ‘list’ );
    $config->set( ‘template.layout.type’, ‘list’ );
    $config->set( ‘post.preview.media.width’, 30 );

    get_header(); ?>

    <div id=”content” class=”content” role=”main”>

    <?php if ( have_posts() ) : ?>

    <div class=”articles-list”>

    <?php do_action( ‘presscore_before_loop’ ); ?>

    <?php update_post_thumbnail_cache(); ?>

    <?php
    $count = 0;
    $your_repeater = get_field(‘sub_seminars’);
    if($your_repeater){
    while( have_rows(‘sub_seminars’) ): the_row();
    $count++;
    $my_field = get_sub_field(‘start_date’);
    if ($count == 1) { ?>

    <?php while ( have_posts() ) : the_post(); ?>

    <?php
    // populate config with current post settings
    presscore_populate_post_config();

    presscore_get_template_part( ‘theme’, ‘blog/list/blog-list-post’ );
    ?>

    <?php endwhile; ?>

    <?php } endwhile; } ?>

    <?php do_action( ‘presscore_after_loop’ ); ?>

    </div>

    <?php dt_paginator(); ?>

    <?php else : ?>

    <?php get_template_part( ‘no-results’, ‘blog’ ); ?>

    <?php endif; ?>

    </div>

    <?php get_sidebar(‘primary’); ?>

    <?php get_footer(); ?>`

  • I want something similar. But can’t figure out how to do it.

    <?php
    
    /**
     * The main template file.
     *
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists.
     * Learn more: http://codex.wordpress.org/Template_Hierarchy
     *
     * @package presscore
     * @since presscore 0.1
     */
    
    // File Security Check
    
    if ( ! defined( 'ABSPATH' ) ) { exit; }
    
    $config = Presscore_Config::get_instance();
    $config->set( 'template', 'blog' );
    $config->set( 'layout', 'list' );
    $config->set( 'template.layout.type', 'list' );
    $config->set( 'post.preview.media.width', 30 );
    
    get_header(); ?>
    
         
    			<div id="content" class="content" role="main">
    
    				<?php if ( have_posts() ) : ?>
    
    					<div class="articles-list">
    					
    						<?php do_action( 'presscore_before_loop' ); ?>
    
    						<?php update_post_thumbnail_cache(); ?>
    
    						    <?php
      							        $count = 0;
    								$your_repeater = get_field('sub_seminars');
    								if($your_repeater){
       								while( have_rows('sub_seminars') ): the_row();
       								$count++;
       								$my_field = get_sub_field('start_date');
       							    if ($count === 1) { ?>
    
    						        <?php while ( have_posts() ) : the_post(); ?>
    								                                                
                                                            <?php
    							// populate config with current post settings
    							presscore_populate_post_config();
    
    							presscore_get_template_part( 'theme', 'blog/list/blog-list-post' );
    							?>
                                                  																	                                                  
    						       <?php endwhile; ?>
     
                            <?php } endwhile;  } ?>
    
                            
    
    						<?php do_action( 'presscore_after_loop' ); ?>
    
    					</div>
    
    					<?php dt_paginator(); ?>
    
    				<?php else : ?>
    
    					<?php get_template_part( 'no-results', 'blog' ); ?>
    
    				<?php endif; ?>
    
    			</div>
    
    			<?php get_sidebar('primary'); ?>
    						 		  
    <?php get_footer(); ?>

    Can you tell me why my function runs for count == 1 only even if I put count == 2 it won’t run

  • add_filter('acf/load_value/name=start_date', 'delete_old_courses_by_date');
    function delete_old_courses_by_date($rows, $post_id, $field) {
      if (!is_array($value) || !count($value)) {
        return $value;
      }
      // get the current date
      $now = date("Ymd");
    
      // set up a new array to hold the values we keep
      $new_value = array();
      foreach ($rows as $row) {
        // the php strtotime() function could fail depending on
        // the return format of the date/time fields
        // this requires a valid date/time format as documented here
        // http://php.net/manual/en/datetime.formats.php
        // if this does not work I probably won't be much help figuring
        // our how to covert your return value to something usable
      $start = date("Ymd", strtotime(get_sub_field('start_date')));
        if ($start > $now) {
          $new_value[] = $row;
        }
      }
      return $new_value;
    }

    Look at this code. Can you figure out what is wrong with it.

    The fields look like this in admin

    ScreenShot

  • ACF default format is “Ymd”

    I have used strtotime() before at a different page and it worked fne for me there.

    Something is wrong with it here that i don’t know what

    Thanks for your help.

  • Thanks again John.

    My date format is YMD

    The output date in in this format “20170313”

    But this still doesn’t work for me.

  • Thanks John
    You are awesome. The code works if I put repeater field name as start_date, all the start_date rows are deleted whether the date is valid or not, it deletes everything, may be the time() function and strtotime() are not in sync, My date output format is default Ymd format, but I would like to know if you please, I have a Nested Structure MY ACF field Name is Sub Seminars ,then I have another sub seminars field inside which is a repeater field containing Start_date and End_date.

    I only need some tweaks in the code that you provided so generously to get it working.

    Thanks for the reply.

  • Thanks John.

    I forgot to mention that I am not so good when it comes to coding. Any chance you can give me a woring example.

    Below is the code I am using to display the fields.

    ?php 
        
    if( have_rows('sub_seminars') ):
    
        while( have_rows('sub_seminars') ) : the_row(); 
            
            ?>
             
    			              
    
    	    <tr>
    	   
            <td style="border: 1px solid #ddd; width: 165px;"><h6><?php the_sub_field('start_date'); ?></h6></td>
            <td style="border: 1px solid #ddd; width: 165px;"><h6><?php the_sub_field('end_date'); ?></h6></td>
    </tr>
    <?php
    			
        endwhile;
    
             endif;
    
            ?>
  • okay I finally figured it out how to display them using the documentation provided by ACF. Thanks

Viewing 20 posts - 1 through 20 (of 20 total)