Support

Account

Forum Replies Created

  • I found a work around, i suppose, but if you can tell me if this is the way i am supposed to use this field type, that would be helpful.

    So to solve my problem, i wrapped every Relationship field query in a custom WP_QUERY loop. I pull the page/post id to load the current pages fields.

    I’m thinking its better to use wp_query and not the wordpress loop. Doing it this way does seem to make it not ruin $post->ID for other sections of the page.

    Here is a sample code from my page…i do the same thing in the header, page.php and sidebar.php – I’ve setup section on each page that will load a post that is assigned to it (I’m using to show banner ads in different spots of the page based on the settings the user makes. They can choose the banner ad per page).

    <?php 
    $postid = get_the_ID();
    
    $homepgqry = array( 
    	'p' => $postid,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $homepgqry );
    while ( $loop->have_posts() ) : $loop->the_post();
     
    $posts = get_field('homepage_promo_bottom');
    
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php //setup_postdata($post); ?>
            <?php 
    					$attachment_id = get_field('promo_image');
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url'); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
        <?php } endforeach; ?>
        <?php // wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    
    <?php endwhile; wp_reset_query(); ?>
  • I suppose I try to limit the number of the plugins i install on any website because if you load up your site with too many plugins it can cause the site to load slower or occasionally you may run into conflicts. I have a core set of plugins that i use alot…maybe 5 or so. ACF Pro, Reveal Template, Yoost SEO Plugin, and a couple others.

    if i can avoid installing a plugin, i will. I don’t like wondering if when i update the plugin if its going to break my website.

    But, since you talked to the developer of the plugin you are using and are happy with that route, that should work just fine for you. Good luck!

  • I would recommend not using a plugin to create custom post types.

    You can register custom post types by adding a function to your themes functions.php file.

    Here’s an example:

    // REGISTER CUSTOM POST TYPES
    // You can register more, just duplicate the register_post_type code inside of the function and change the values. You are set!
    if ( ! function_exists( 'create_post_type' ) ) :
    
    function create_post_type() {
    	
    	// You'll want to replace the values below with your own.
    	register_post_type( 'genstarter', // change the name
    		array(
    			'labels' => array(
    				'name' => __( 'Gen Starter' ), // change the name
    				'singular_name' => __( 'genstarter' ), // change the name
    			),
    			'public' => true,
    			'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ), // do you need all of these options?
    			'taxonomies' => array( 'category', 'post_tag' ), // do you need categories and tags?
    			'hierarchical' => true,
    			'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
    			'rewrite' => array ( 'slug' => __( 'genstarters' ) ) // change the name
    		)
    	);
    
    }
    add_action( 'init', 'create_post_type' );
    
    endif; // ####
  • I found this article:
    http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    I’ll give this a whirl, I think a select drop down menu would work just as well. Thanks.

  • No manual month changes. I can handle that easily with some date variable manipulation.

    On my site i have a widget that uses AJAX to send the month and year to the page. I grab those values and make some date variables.

    Mainly i look at today and get the month and year. I make a variable for the first of the month like this: 20140601 — i store that value in a variable. Then i also get the last day of the given month and make my ending day variable with that value. I use php “t” value to get the last day of the month.

    That code ends up looking like this:

    $current_month = str_pad($_GET['_m'], 2, '0', STR_PAD_LEFT);
    $current_day = "01"; // day one
    $current_year = $_GET['_y'];
    	
    $get_last_day = $current_year.$current_month.$current_day;
    $lastday = date("t", strtotime($get_last_day));
    	
    $tempstartday = $current_year.$current_month.$current_day;
    $tempendday = $current_year.$current_month.$lastday;
    	
    $startday = date('Ymd', strtotime($tempstartday));
    $endday = date('Ymd', strtotime($tempendday));

    In my final code, i pass the $startday and $endday to the wordpress query in each of those spots where in the example I have the date hardcoded.

    'value' => $startday,

  • I don’t see any difference between date(‘Ymd’) and current_time(‘Ymd’); They both output todays date.

    It seems to do what I wanted this to do was even more involved. The tricky part was when an event spanned multiple months. Say an Event would start in May and end in July. The event would not show up in June because it was out of the scope of the range of the event.

    So, a very smart fellow by the name of “keesiemeijer” over at wordpress.org helped me get the query correct.

    Here is the solution:

    Add this to your functions.php file – modify as needed:

    function get_meta_sql_date( $pieces, $queries ) {
        global $wpdb;
    
        // get start and end date from query
        foreach ( $queries as $q ) {
    
            if ( !isset( $q['key'] ) ) {
                return $pieces;
            }
    
            if ( 'event_start_date' === $q['key'] ) {
                $start_date = isset( $q['value'] ) ?  $q['value'] : '';
            }
            if ( 'event_end_date' === $q['key'] ) {
                $end_date = isset( $q['value'] ) ?  $q['value'] : '';
            }
        }
    
        if ( ( '' === $start_date ) || ( '' === $end_date ) ) {
            return $pieces;
        }
    
        $query = "";
    
        // after start date AND before end date
        $_query = " AND (
            ( $wpdb->postmeta.meta_key = 'event_start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s) )
            AND ( mt1.meta_key = 'event_end_date' AND ( CAST(mt1.meta_value AS DATE) <= %s) )
        )";
        $query .= $wpdb->prepare( $_query, $start_date, $end_date );
    
        // OR before start date AND after end end date
        $_query = " OR (
            ( $wpdb->postmeta.meta_key = 'event_start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
            AND ( mt1.meta_key = 'event_end_date' AND ( CAST(mt1.meta_value AS DATE) >= %s) )
        )";
        $query .= $wpdb->prepare( $_query, $start_date, $end_date );
    
        // OR before start date AND (before end date AND end date after start date)
        $_query = " OR (
            ( $wpdb->postmeta.meta_key = 'event_start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) )
            AND ( mt1.meta_key = 'event_end_date'
                AND ( CAST(mt1.meta_value AS DATE) <= %s )
                AND ( CAST(mt1.meta_value AS DATE) >= %s )
            )
        )";
        $query .= $wpdb->prepare( $_query, $start_date, $end_date, $start_date );
    
        // OR after end date AND (after start date AND start date before end date) )
        $_query = "OR (
            ( mt1.meta_key = 'event_end_date' AND ( CAST(mt1.meta_value AS DATE) >= %s ) )
            AND ( $wpdb->postmeta.meta_key = 'event_start_date'
                AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s )
                AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s )
            )
        )";
        $query .= $wpdb->prepare( $_query, $end_date, $start_date, $end_date );
    
        $pieces['where'] = $query;
    
        return $pieces;
    }

    Then add this to your page where you do your query.

    <?php
    add_filter( 'get_meta_sql', 'get_meta_sql_date', 10, 2 );
    $all_events = array (
        'post_type' => 'events',
        'posts_per_page' => 50,
    
        // creates the meta sql join and where clauses
        // which will be filtered in functions.php
        // must be two meta_query arrays
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'       => 'event_start_date',
                'compare'   => '>=',
                'value'     => '20140601',
                'type'      => 'DATE'
            ),
            array(
                'key'       => 'event_end_date',
                'compare'   => '<=',
                'value'     => '20140630',
                'type'      => 'DATE'
            )
        ),
    );
    
    $date_query = new WP_Query( $all_events );
    remove_filter( 'get_meta_sql', 'get_meta_sql_date', 10, 2 );
    ?>

    So, holly molly was this involved. I would recommend that they add a similar solution to the documentation on the ACF website.

    This worked for me.

  • In my custom field, I am using yymmdd (all lowercase) – I see it stored in the database like 20140615, for example.

    I added current month, current day, current year variables because I have a widget on my website that shows events that occur in a single month.

    Do you mind posting your code and let me know what settings you used in the custom field too?

  • You can try this… I had the question posted in multiple places.

    https://wordpress.org/support/topic/query-custom-post-types-by-two-custom-date-fields?replies=11#post-5701103

    I wont be able to test this until tonight, but maybe it will help you get started. Let us know if it works.

  • I have not come up with a solution to this problem. Instead I just went back to the event plugin we were using before as that did what I need it to do. I only had a limited amount of time to work on it.

    I would love to know the answer to this problem for future reference, so if someone has a solution, please post it here.

  • $all_events = array (
        'post_type' => 'events',
    		'posts_per_page' => 50,
    		'status' => 'published',
    		'meta_query' => array(
    		'relation' => 'OR',
        array(
            'key' => 'event_start_date',
            'value' => array( $startday, $endday ),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
     ),
    );

    I’ve tried this but it doesnt exactly work.

    I saw in your documentation there is a way to query posts between dates. So i tried that, but it doesnt quite seem to work with what i want to do with it.

    I have some events and i made a start event date and end event date fields. I want to query the events by the start and end dates.

    The problem i have is lets say an event starts in May and ends in July. I have a page that lists the events by month. A page for May, June, and July. So this event that has a start date of May 1 2014 and an end date of July 28, 2014 should also show on the June page.

    Do you have any options for getting the query to work in this way? It doesnt seem to be working.

  • Look in the wp_postmeta table. ACF stores the custom fields in the same way that WordPress would store a custom field. More specifically you’ll a key and value columns. The key is the name of the custom field and the value is the value you entered into the field.

  • Does this look correct? I’m getting good results with it. So, i think i got it.

    $plfsByState = $wpdb->get_results($wpdb->prepare( 
    "
    SELECT pm.meta_value as state, count(*) as postcount 
    FROM $wpdb->posts p
    			join $wpdb->postmeta pm on p.ID = pm.post_id
    where p.post_type = 'plfmaps'
    and p.post_status = 'publish'
    AND meta_key LIKE %s 
    AND meta_value LIKE %s 
    GROUP BY meta_value ASC",
    'locations_%_state',
    '%',
    ""		
    ));

    Thanks for your help. This appears to be showing all of the states, grouped by the state and then showing a count of the total number of results from that state. This is a little tricky with those % signs.

    Question about the query. At the very end, ‘locations_%_state” is the meta_key, and ‘%’ is the meta_value, so then what is the “” at the very end? What does that do?

  • hmm…let me try that last one out…let me see what i can get.

  • Thanks jarvis. I probably can’t use the first query because then i would have specify each state in the query and we will be constantly adding new company locations, so i kind of want it to work a bit organically.

    The second one, i am going to try that out now.

  • $plfsByState = $wpdb->get_results("
        select pm.meta_value as state, count(*) as postcount
          from $wpdb->posts p
               join $wpdb->postmeta pm on p.ID = pm.post_id
         where p.post_type = 'plfmaps'
           and p.post_status = 'publish'
      group by state
      order by state ASC");

    This gives me way too much data…And doesnt exactly work. This basically returns everything. I’m seeing div with the address, city, state. How can i modify this query to only show the states?

    Where I am getting mixed up is the meta_key value for the location_state actually looks like this in the database table “locations_0_state” and “locations_1_state” — any ideas on this one? I feel like I am really close, but I’m missing something.

  • I think the query i am after is a little more complicated. I am trying to do a GROUP BY on a repeater field subfield value.

    My data is setup like this:

    custom post type called “companies”.
    In the post type i have a repeater field called “Locations”.
    In the locations repeater field I have address, city, state, zipcode — all of the address information.

    I am building a map so I want to show all results grouped by state. Ideally, I’d be able to show what all states are in my repeater field and show the count of how companies are in that location.

    I’m finding it a bit tricky to group by the repeater state sub_field that I’ve created. Any ideas how to do that?

Viewing 16 posts - 26 through 41 (of 41 total)