Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • This is quite interesting question! It would be trully handy if there would be tokens to display in message box.

  • Hi!

    I think I understand, you’re probably trying to query by a custom field.

    Looking at Example 1. from that resource page, it’s very similar to what you are trying to accomplish. Assuming your custom field is attached to some kind of query-able post type like posts or pages (my preference is to create a custom post type) this should be very easy.

    
    <?php 
    
    // args
    $args = array(
    	'posts_per_page' => -1, //get all posts
    	'post_type'	 => 'your_post_type_slug', //posts, pages, whatever the field is attached to
    	'meta_key'	 => 'listing-location', //custom field slug
    	'meta_value'	 => 'New York' //location to count
    );
    
    // query
    $the_query = new WP_Query( $args ); //get WP Query using $args
    
    $count = $the_query->post_count; //use property post_count to return the count
    
    echo 'Location: New York';
    echo 'Total listings I have with that location are '. $count; 
    ?>
    

    The property post_count is available when using a WP_Query. More information here: WP_Query

    Obviously this may not be the most efficient code because the location is hard coded, but maybe that works in your situation.

    My advice would be to set up the listing location as a custom category/taxonomy instead of an ACF field. That way you could loop through all the available categories dynamically.

    Hope this helps!

  • @pipoulito That is the format ACF stores dates. You should contact the developer of Revolution Slider to figure out how to use ACF fields with their plugin. I can tell you what’s ACF stores for a field but cannot help you use it in the other plugin.

    Date fields should be sorted either as numbers or text, but not as dates.


    @ryanacarty
    yes, Rev Slider is a commercial plugin that requires a license to update. Some themes package it and include it but not of them include a license for this plugin.

  • If it’s being updated with the right value then I haven’t got a clue why your query is not working. My next step would be to look in the database to see exactly what is being saved in that field.

  • Thanks for your response @hube2 ! I’ll try to fill in:

    The fields are set like this:

    $updated_balance = get_field('updated_balance', $post_id);
    $transaction_date = get_field('transaction_date', $post_id);
    $account = get_field('transaction_account', $post_id);

    Where updated_balance is a number field, where transaction_date is a date field and transaction_account is a post object field selecting a ik_account CPT.

    All these fields are set on a new ik_balance CPT post when a ik_transaction CPT post is saved.

    So, in the query, I’m first looping through all posts of the ik_account CPT, getting only the post ID which is what’s populating $account_id. Inside that loop, the above query fetches the last ik_balance CPT where the balance_account post object field equals $account_id.

    Hope this makes things at least somewhat more clear. Otherwise I’ll try to clarify further!

  • Thanks John, I ended up solving it, my working code ended up being:

    <?php $accommodation_relationship = get_field( 'accommodation_relationship' ); ?>
    <?php if ( $accommodation_relationship ): ?>
    <?php foreach ( $accommodation_relationship as $post ):  ?>
    <?php setup_postdata ( $post ); ?>
    
    <?php
    if( have_rows('acommodation_location_repeater') ):
    while ( have_rows('acommodation_location_repeater') ) : the_row();
    ?>
    
    <div class="col-md-3 accommodation">
    <h3><?php the_sub_field( 'accommodation_name' ); ?></h3>
    <p class="description"><?php the_sub_field( 'accommodation_description' ); ?></p>
    <p class="address"><?php the_sub_field( 'accommodation_address' ); ?></p><br />
    <a href="tel:<?php the_sub_field( 'accommodation_phone' ); ?>" class="contact-link"><?php the_sub_field( 'accommodation_phone' ); ?></a><br />
    <a href="mailto:<?php the_sub_field( 'accommodation_email' ); ?>" class="contact-link"><?php the_sub_field( 'accommodation_email' ); ?></a>
    <?php $accommodation_website = get_sub_field( 'accommodation_website' ); ?>
    <?php if ( $accommodation_website ) { ?>
    <br />
    <button class="accommodation-bttn"><a href="<?php echo $accommodation_website; ?>">Website <i class="fas fa-chevron-right"></i></a></button>
    <?php } ?>
    </div>
    
    <?php endwhile; else : endif; ?>
    
    <?php wp_reset_query(); ?>
    
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    Thanks!

  • Update II: In response to the issue of my thumbnails not displaying; I have figured this out if anyone else with similar issues stumbles into this…

    Basically, for me, I eliminated the then unnecessary pre_get_posts filter and then replaced;

    <?php $image = wp_get_attachment_image_src(get_field('event_thumbnail'), 'full'); ?>
    <img src="<?php echo get_field('event_thumbnail'); ?>" alt="<?php echo get_the_title(get_field('event_thumbnail')) ?>" />

    with:

    <?php $image = get_field('event_thumbnail'); ?>
    <img src="<?php echo $image; ?>" alt="<?php echo get_the_title(get_field('event_thumbnail')) ?>" />

    For some reason (probably some conflict again), wp_get_attachment_image_src returns as false. But get_field('event_thumbnail') returns the image anyway, so all that was needed was to remove this wp_get_attachment_image_src function!

    Hope this helps someone!

  • You cannot use “IN” and must use “LIKE” as @acf-fan says and you need to have a nested query and do multiple “LIKE”s

    
    'meta_query' => array
      array(
        'relation' => 'OR',
        array(
          'key' => 'your-field',
          'value' => 'value 1',
          'compare' => 'LIKE'
        ),
        array(
          'key' => 'your-field',
          'value' => 'value 2',
          'compare' => 'LIKE'
        ),
        array(
          'key' => 'your-field',
          'value' => 'value 3',
          'compare' => 'LIKE'
        ),
      )
    )
    

    This can be costly to performance and it the list you are looking for is too long could even time out your site. This old post is about repeaters but it also applies in cases where ACF stores values as serialized arrays https://web.archive.org/web/20190814230622/https://acfextras.com/dont-query-repeaters/

  • This of course didn’t really solve my problem of having editorial staff with different language browsers – but at least now I know what causes the problem.

    I read that adding a language marker to the field could force the numbers’ format… like this:

    <input type="number" lang="en">

    Is there any way to add such a language marker to the number field?

    Thank you very much!

  • Don’t know if the OP every got this worked out, but it really isn’t possible without doing several queries. Even querying the DB directly would require several queries. Basically you need to get a list of post IDs to use in a second query.

  • Adding rows to a sub field requires using add_row().

  • Ha… well I came up with a solution.

    Let’s see… I kind of have to piece it back together. I got it working nearly a year ago and haven’t given it much thought since then.

    First, I’m using register_activation_hook() to check if ACF (free) is active. If it is, I’m copying a file into mu-plugins that contains the following, to deactivate ACF (free):

    
    add_filter('option_active_plugins', function($plugins) {
    	$key = array_search('advanced-custom-fields/acf.php',$plugins);
    	if ($key !== false) {
    		unset($plugins[$key]);
    	}
    	return $plugins;
    });
    

    That register_activation_hook() also generates an admin notice explaining that ACF (free) has been deactivated because my plugin includes ACF PRO. I also have a “Bypass ACF Check” option I created in my plugin’s settings, that will prevent this from running, so they can keep ACF (free) active if they really want to — but explaining this means they lose the ACF PRO features my plugin depends on.

    There’s a corresponding register_deactivation_hook() to delete my file from mu-plugins and re-activate ACF (free), although honestly I don’t think it’s working properly. (I haven’t prioritized fixing it because none of my users has yet complained about it!)

    So, that’s the first step, getting ACF (free) out of the way. Next we have to actually load the PRO version. I’m doing that with the plugins_loaded action, calling this function. (Actually it’s a method in my main class.)

    Note right away here that I’m checking to make sure the site doesn’t already have ACF PRO installed, because if it does I just use that and don’t load my own copy.

    
    		public function acf_init() {
    			// Only load our version of ACF PRO if the full version is not already installed on the site
    			if (!is_plugin_active('advanced-custom-fields-pro/acf.php')) {
    				include_once(plugin_dir_path(__FILE__) . 'vendors/acf/acf.php');
    				add_filter('acf/settings/url', function($url) {
    					return plugin_dir_url(__FILE__) . '/vendors/acf/';
    				});
    				// Hide ACF editing screens (EXCEPT if the site also has ACF free installed)
    				if (!file_exists(WPMU_PLUGIN_DIR . '/r34icspro-no-acf.php')) {
    					add_filter('acf/settings/show_admin', function($show_admin) {
    						return false;
    					});
    				}
    			}
    			// Add our ACF JSON loading path
    			add_filter('acf/settings/load_json', function($paths) {
    				$paths[] = plugin_dir_path(__FILE__) . 'assets/acf-json';
    				return $paths;
    			});
    		}
    

    Some of this may require some modifications (e.g. you may not have the assets/acf-json folder in your plugin, or you may have acf-json somewhere else, and your file in mu-plugins definitely won’t be called r34icspro-no-acf.php); I also stripped out some code that exists for a “dev mode” I have in my plugin.

    I think that’s the heart of it. Then the other big thing I would just recommend is that you should definitely stay on top of keeping your embedded ACF PRO version up-to-date! It isn’t critical for users who have their own version of ACF PRO, but if you’re knocking out their copy of ACF (free), then it’s on you to make sure they’re getting ACF updates. (Of course, some sites might be deliberately holding their versions back, and that’s not something I’ve accounted for here.)

  • Hey, have you managed to solve/finalize this project? Happy nu year 😉
    I would guess it needs to be done two separate query, using some results from the #1 query to feed the #2. I did smt similar before, where i only wanted to show the images that the photographers placed into their galleries alr.

  • Like so many problems, a weekend off and a fresh look solved things. My initial code must have had a error in it as fixing it turned out to be super simple:

          <?php 
                            $terms = get_field('featured_topics');
                
                    
                            if( $terms ): ?>
                            <?php foreach( $terms as $term ):
                            
                            ?>
           
            <h5 class="text-center"><?php echo esc_html( $term->name ); ?></h5>
            <p><?php the_field('s_t_intro', $term); ?></p>
            <a href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?> blog posts</a>
                <?php endforeach; ?>
                <?php endif; ?>

    Hopefully that might help someone in the future.

  • … and ofcourse, a few minutes after posting my question I find a solution. For those who is trying the same thing, this link helped me out.

    Next thing for me to do is making the titles of the new columns clickable so that the overview can be ordered accordingly.

  • I have had the same issue in a query.

    Try

    ‘compare’ => ‘LIKE’

    Hope that helps…

  • If I would for some reasons convert to relationship can I still use get_field without post_object->ID? As I prefer get_field queries to structure the quries before echo them over the_field directly into the echo.

    Just having a quick look at https://www.advancedcustomfields.com/resources/relationship/ where the_field is used once using setup_postdata();. Just did a quick test but even if I setup setup_postdata(); I need to call

    within the foreach loop:

    		$imageArray_BG = get_field('post_hero_img_1', $post_object->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG = esc_attr($imageArray_BG['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG = esc_url($imageArray_BG['url']); // Grab the full size version 

    Which means all posts in the relationship is not passed in one query, correct?

  • I honestly don’t know if there is a way to tell if meta values have already been cached. When I’m trying to figure this out I generally install a query monitor plugin and test first with and then without the call to get_post_meta().

    Using https://wordpress.org/plugins/query-monitor/ does any of them actually say based on real queries cached or not? Using both but right now using https://wordpress.org/plugins/blackbar/

    If the queries are reduced I leave it in.

    What do you mean with leave it in? If queries is reduced it means not cache, or am I misunderstanding you or just getting late here..

    Another thing you might consider is if you’re actually going to use the post data. Will you use the post title, excerpt, content, ect, OR will you only be using custom fields from those posts. If you will not be using standard post field content then you don’t really need the post object and just the ID would be needed to get the custom fields. You can do this by either setting the field to return an array or by get_field('post_object', false, false);

    In this I only use custom fields. Could you give me a full visual example as I don’t think I understand what you mean with “or by get_field('post_object', false, false);

  • For those of you still struggling with this problem, I’ve found a super light and easy-to-use plugin that works like charm. It’s called Edit Custom Fields: https://wordpress.org/plugins/edit-custom-fields/. It lists all custom fields DB entries automatically (both, active and old), allowing you to rename or delete them on a per-name basis. No need of MySQL tweaking!

  • Dots vs commas is controlled by and built into the browser and is not a function of ACF, all ACF does is output <input type="number"> What the browser does is dependent on where the user is and that person’s browser setting for language, in some browser.

  • If I needed to use 20+ post object fields in a single page, depending on how often the posts selected would be changed, if this was causing a performance issue, I would find a way to cache the results, for example storing all 20+ results in a single transient that could be loaded using a single db call.

    I honestly don’t know if there is a way to tell if meta values have already been cached. When I’m trying to figure this out I generally install a query monitor plugin and test first with and then without the call to get_post_meta(). If the queries are reduced I leave it in.

    Another thing you might consider is if you’re actually going to use the post data. Will you use the post title, excerpt, content, ect, OR will you only be using custom fields from those posts. If you will not be using standard post field content then you don’t really need the post object and just the ID would be needed to get the custom fields. You can do this by either setting the field to return an array or by get_field('post_object', false, false);

  • Hi John!

    Reading post object, in the end it will be around 20+ different post objects and 5-10 different categories. Each category will have 5-20 post objects.

    See below if this make sense.

    $post_object = get_field('post_object_categoryONE_1','options')
    $post_object_2 = get_field('post_object_categoryTWO_1','options')

    And this is the code part we are referring to:

    $post_object = get_field('post_object_categoryone_1','options'); // Set field to grab
    if( !empty ($post_object) );
            $imageArray_BG = get_field('post_hero_img_1', $post_object->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG = esc_attr($imageArray_BG['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG = esc_url($imageArray_BG['url']); // Grab the full size version

    One post object contain one post. I see now what you mean, relationship would be smarter code and query once to receive all meta. I didn’t think about each new DB queries here. But in this case I need to fetch one post per post object strictly even if it from the side doesn’t make sense.

    Regarding the conditions it displays a different post object category based on which category the page have selected which is a select field on the page. Like if we would have 20 post objects fruits and 20 post object vegetables. Then the first post object in fruits would show apple while if the category was vegetables it would show carrot as the first post object field.

    But with what you said I should maybe skip conditions here and make 10 different blocks as this would reduce the DB queries, as each block would then represent the conditions. That way I would limit the calls to one extent. Which maybe wouldn’t have been an issue with the relationship field as you explained using one call to fetch all posts in one query.

    But looking at a whole page with other plugins DB calls such as WPML and other together with other page custom fields the amount of fields maybe is much it’s relative I guess, the number must be compared to each function what you try to do to determine if it’s much or not. But logged in I have just as an example SQL (233 queries|62.61 ms). It might still be ways to optimize the DB queries here even if I’m not going with the relationship fields, as one example as you suggested below regarding get_post_meta()

    Is there an easy way to check if the queries is cached or not? I also use redis object cache. But I anyhow wouldn’t prefer to rely on cache it should be beneficial not a must.

    This can be simulated by doing get_post_meta($post_object->ID); which results in the same thing, WP gets all of the meta values for the post in a single query and caches them.

    Could you show me this in one of my examples above, I didn’t make it work. Where would you like to place it?

    Thanks for the answer regarding if/elseif/else and switch conditions.

  • Hello Everyone, I was facing the same issue, trying to include a read-more button on a textarea field, I tried to switch it to wysiwyg and then create the read more, but it created unwanted p tag. I use javascript-plugin oriented solution instead and not a PHP/Wordpress oriented solution.

    i’m using this Readmore.js plugin
    https://github.com/jedfoster/Readmore.js/tree/version-3.0

    Which we can configure with 2 steps:
    1 – adding specific class to the the field we want to add the read more (textearea in my case)
    2 – write the script for the readmore-js to work

    // My selector where my textarea field is included
    const readMoreParagraphs = document.querySelectorAll('.item-texte');
    
    new Readmore( readMoreParagraphs, {
        speed: 200,
        collapseHeight: 20,
        lessLink: '<a href="#" class="read-more">Read less</a>',
        heightMargin: 16,
        moreLink: '<a href="#" class="read-more">Read more</a>',
    });
  • Well…talk about ugly. What I did was after figuring out if it was a new post or an edit, I created a third tab and placed the selects (that I don’t want the user to see) inside the tab. I then remove the tab by means of jQuery and everything works as expected.

    If anyone has a better solution than this, please chip in. One of my selects calls an endpoint to load its values, I would love to get rid of this overhead when editing the post as that select is no longer needed. Thanks!

    		if ( PostHelper::is_current_page_new_post() ) {
    			$afb->add_adoption_type_select_field();
    			$afb->add_adoption_animal_select_field();
    			$afb->add_adoption_person_select_field();
    		} else {
    			$afb->add_adoption_type_text_field();
    			$afb->add_adoption_animal_text_field();
    			$afb->add_adoption_person_text_field();
    		}
    		$afb->add_tab_field( 'Hidden', 'hidden' );
    		if ( ! PostHelper::is_current_page_new_post() ) {
    			$afb->add_adoption_type_select_field();
    			$afb->add_adoption_animal_select_field();
    			$afb->add_adoption_person_select_field();
    		}
  • Do you have anything in your console output? Any jQuery errors?

Viewing 25 results - 6,051 through 6,075 (of 21,332 total)