Support

Account

Forum Replies Created

  • In case anyone else needs a solution to this specific issue, here is what worked to hide the nag.

    I added this small snippet of code to my Child Theme’s functions.php file:

    add_filter( 'acf/admin/prevent_escaped_html_notice', '__return_true' );

    If you use this, please be sure to FIRST check your usage of (ACF’s) the_field, which now escapes any HTML in the contents of the_field, and ensure that you won’t have any site-breaking issues.

  • Respectfully I echo er.mayankrajput’s suggestion for consideration in a future update…..an option to delete – or not – the data already stored in the DB that is associated with the field (or repeater row) being deleted.

  • Likely the addition of the meta_type, but YAY! I’m glad it’s working for you! šŸ™‚

  • @hube2 thank you for the explanation! I’m also so accustomed to typing ‘array’ that I don’t even think about it anymore….but it’s good to know the shortcut.

    In the OP’s case, then it looks like his query is written correctly *except* that my understanding is that “LIKE” is for strings, but the meta_value looks like a number, so perhaps adding the meta_type and changing the operator to ‘=’ should work?

    Like so:?

    'meta_query' => [
            'key' => 'program_id',
            'value' => 5317,
            'type' => 'numeric',
            'compare' => '='
    
               ],

    OR alternatively just do the query without using meta_query, like so:

    $student_query_args = [
        'post_type' => 'student_list',
        'post_status' => 'publish',
        'posts_per_page' => 100,
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'meta_key' => 'program_id',
        'meta_value' => '5317'
    ];
  • Iā€™m not an expert on this but I did something similar – added columns for a CPT and made them sortable……so a couple of suggestions (questions?) come to mind:

    1. I assume you specified that the (new) columns are sortable using the hook “manage_edit-{cpt}_sortable_columns’? I have three added columns for a ‘deals’ CPT so I made those columns sortable like this (example):

    function my_deal_sortable_columns( $columns ) {
    	$columns['expires'] = 'expires';
    	$columns['author'] = 'author';
    	$columns['resort'] = 'resort';
    	return $columns;
    }
    add_filter( 'manage_edit-deal_sortable_columns', 'my_deal_sortable_columns' );

    2. Perhaps you need to add the meta_type to your orderby statement? I need the ‘expires’ column to be sortable based on the value which is a date, not a string, so I had to add the meta_type, like this example:

      $orderby = $query->get( 'orderby');
     
        if( 'expires' == $orderby ) {
            $query->set('meta_key','booking_date_end');
            $query->set('orderby','meta_value');
    		$query->set('meta_type','DATE');
    		$query->set('ignore_sticky_posts',true);
        }

    Hopefully this may be enough to help, if not post back and maybe I can be of more assistance. Good luck!

  • It should be possible by simply modifying the template file that displays your home page, and that is best done in a Child Theme unless your Theme is completely custom and you don’t have to worry about template files being overwritten by Theme updates.

    SO first identify your Child Theme’s home page template (many themes use different names such as homepage.php, frontpage.php, or even index.php – you’ll have to figure out which one it is, if you’re having trouble way at the bottom I have a snippet you can add that tells you which template file is being used on any page).

    Then find the spot in the template where the_excerpt(); appears – MANY newer Themes use a ‘content’ loop so the template MAY refer you to another template (part) like this:

    get_template_part( 'content','specific-content-type' );

    If it does then open up that content file and look for the_excerpt(); there.

    Then all you have to do is insert your custom field variables either before or after the excerpt, or even replace the excerpt with your custom field data if you don’t want to use an actual excerpt (say for a short product description, which you can either pull from the_content(); or use the actual excerpt field on the Post Edit screen, if it’s not showing make sure it’s checked under “Screen Options” at the top of the page)

    If you need help in how to add your custom field data to the template, let me know and I can give you some code.

    — IF YOU CAN’T figure out what template file is being used, add THIS CODE to your Child Theme’s functions.php file OR your own custom plugin, you can comment it out or uncomment as necessary, or just leave it uncommented all the time – this will ONLY display the template file used IF you are a logged-in Admin-level user, it shows at the TOP of the page above the header the path & file name:

    // Debug function to show the name of the current template being used
      function show_template() {
        if ( current_user_can( 'create_users' ) ) {
          global $template;
          print_r($template);
      	}
       }
      add_action('wp_head', 'show_template');
  • Let me sure I understand, because the default WP behavior is to link the ‘read more’ to the individual Post already….but you want to override this and have it link elsewhere?

    If that’s what you want, you can do this easily with a short code snippet in either your Theme’s functions.php file (perfect if you have a Child Theme) or a custom plugin file if you’re not using a Child Theme so that it doesn’t get overwritten with Theme updates.

    There is a WP hook called “the_content_more_link” which you can read about here:
    https://developer.wordpress.org/reference/hooks/the_content_more_link/

    SO in your case you’d setup a custom field to contain this link (there are several to choose from but you could just use the URL field under the Basic section) and have that field appear on all Posts.

    Then your code that goes in your functions.php or custom plugin would be something like this:

    function modify_read_more_link() {
     $morelink = get_field('whateveryourfieldnameis');
     return '<a class="more-link" href="' . $morelink . '">Read More...</a>';
    }
    add_filter( 'the_content_more_link', 'modify_read_more_link' );

    You can change the verbiage (Read More…) to whatever you want, but most readers know what ‘read more’ means so I suggest keeping it simple.

  • OK I think it actually needs the ‘array’, like this:

    'meta_query' => array(
            'key' => 'program_id',
            'value' => '5317',
            'compare' => 'LIKE'
    
               ),

    You may or may not need the single-quotes around the value (‘5317’) it depends on how that custom field is setup (the type of field) and you could also try using ‘=’ in place of ‘LIKE’ but LIKE should work too.

  • Thank you John, excellent suggestion! I have done some dynamic population of select fields so that would not be too hard to implement, I guess I hadn’t thought of that because I’d only ever done it with Posts, not menus, but it makes sense that it would work the same way.

    In the interim I have created templates for each page that does call the menu using wp_nav_menu(), but I’d like to reduce the number of templates needed overall and make it more user-friendly, so I like your solution much better than mine!

    I’ll give this a try later today and will post back my final solution and code examples in case others need to do something similar.

  • Many thanks to @rasso and @tjkelly for their helpful solution – this worked for me!

    NOTE to @iamrobertandrews and anyone else who finds this thread: THIS function as shown above (with your correct user ID and custom post type if you use CPTs) is meant to be a one-time use function – you put it in your functions.php (or a custom plugin if you have one) and then refresh any back-end admin page for it to run, then you can either remove it or comment it out until you need it again.

  • Hi @dunkel My workaround is this:

    In the Field Group I’m using I NOW have the “Minimum Rows” set to zero, so that NO rows are created by default…..when I’m creating a post and I want to ADD rows, then I use the Add Row Button. Often times I don’t.

    If you leave the “Minimum Rows” setting to it’s default (which I think, at least in my version, is 1) then a single row is created for every Post to which the Field Group is applied – this single empty row is what creates the problem and setting the Minimum Rows to 0 fixes that – no row is created hence there is not a ‘1’ applied to the Post in the database (if you have a single row created by a 1 in the field group’s minimum rows setting, even if it’s empty, it appears in the database and if(have_rows) sees it and counts it).

    By changing that to zero, if you’re using the if(have_rows) you will truly have no rows and the code works…..it only finds rows that you manually added.

    I hope this makes sense!

  • Thank you John! Once again, you’ve saved my butt!

    That works perfectly.

  • Eureka!

    To answer your question, yes – 1 row did appear on all ‘new’ posts when created (initially). My theory now is that on the first post, I did use that field for 1 row.

    However, I removed the row from a Post in which it was not being used, so there is no row and just the two field headers and a “Add a Row” button. I updated my instructions for that field to tell authors to start by clicking the Button, which gives them a row to use. If they’re not using that field, they just don’t click the button.

    NOW when I click on “add new” (post) the row is NOT there, just the field names, my instructions, and the button, AND my code now WORKS! It only shows my <h5> header and the list of links IF that field is actually used.

    Also in checking in my database, the field (meta key) is still there (external_resources) with a value of 0 and no sub-fields (rows) which is fine.

    Thank you very much – such a simple fix, it would never have occurred to me to just remove the unused row…..jeesh! <blush>

    šŸ™‚

  • Hi James,

    I deeply appreciate your help!

    Below is the JSON export, and here are three screenshots that I hope you’ll find helpful. Unfortunately I cannot change the Theme or plugins, it’s a live site and the current Theme and plugins provide a good deal of the site’s functionality – the whole thing would break if I switch.

    That said, I really don’t think this is related to the Theme or other plugins, I believe that it’s simply my own inexperience with PHP (I’m a very novice coder) – I just don’t know how to structure my code so that it will show the header (“Additional Resources”) and then links IF there is data entered in at least 1 row, and not show either if there is not – I think looking at the screenshots will help the most, along with my code snippet above.

    Here are the screenshots:
    http://www.bestallinclusive.com/images/0_ScreenShot1.png
    http://www.bestallinclusive.com/images/0_ScreenShot2.png
    http://www.bestallinclusive.com/images/0_ScreenShot3.png

    Here’s my JSON export:

    [
        {
            "key": "group_55f2102aaf128",
            "title": "Post: Additional External Resources",
            "fields": [
                {
                    "key": "field_55f21041f438e",
                    "label": "External Resources",
                    "name": "external_resources",
                    "type": "repeater",
                    "instructions": "<p>Use this field to add OFF-site links and the text anchor for the link. You may use the same for both fields.  This will create a list of links below the post.<\/p>\r\n<p>Examples:<br>\r\nURL: http:\/\/urltomycoolsite.com\/  -  Anchor Text: My Cool Site<br>\r\nOR<br>\r\nURL: http:\/\/urltomycoolsite.com\/  -  Anchor Text: http:\/\/urltomycoolsite.com\/<\/p>",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "external-links",
                        "id": ""
                    },
                    "collapsed": "",
                    "min": "",
                    "max": 10,
                    "layout": "table",
                    "button_label": "Add More Rows",
                    "sub_fields": [
                        {
                            "key": "field_55f210bdf438f",
                            "label": "URL",
                            "name": "external_url",
                            "type": "url",
                            "instructions": "Enter the URL, including the http:\/\/",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "http:\/\/"
                        },
                        {
                            "key": "field_55f21102f4390",
                            "label": "Anchor Text",
                            "name": "external_anchor_text",
                            "type": "text",
                            "instructions": "Enter the Text that will be linked to the URL. May be the same as URL.",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": "",
                            "readonly": 0,
                            "disabled": 0
                        }
                    ]
                }
            ],
            "location": [
                [
                    {
                        "param": "post_type",
                        "operator": "==",
                        "value": "post"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": 1,
            "description": ""
        }
    ]
  • Hi James,

    Yes, it does show 1 (empty) row on the post edit screen, even though I have the minimum set to ‘0’. I don’t know how to make it NOT show a row, but instead just show the “Add Rows” button.

    The problem is that *because* it shows one row, whether or not that row is used, when I look at that post in my database via phpMyAdmin, it shows a value of ‘1’ (1 row) – it should either show ‘0’ or not even be there at all (no meta key field) IF the post author does not fill in anything on that row.

    But because it shows ‘1’ I can’t use that in my query, because the post author *could* use that row so ‘1’ could mean that there are sub-fields, or not. If I use ‘1’ to determine if the section with it’s header should be shown, it will always be shown, even when it hasn’t been used, so I get just the header, no links below, which is not what I want.

    Ideally it should behave as any other custom field – available to use on the Post, but not added to the database as a ‘key’ for that post unless it’s actually used.

    But for now I would be happy if I could just figure out how to query for that metadata, and show the header/section ONLY if data has been filled into the row – my test for data in a sub-field isn’t working. If the row isn’t filled in, I still see the two sub-fields in the database attached to that post, but they are blank….so in theory I could check for that, and if NOT blank, show the header and section, but it they ARE blank, don’t show it. It just isn’t working as I have it.

  • Hi Darrenbachan,

    Here is what I’m doing, I’ve commented it below for the benefit of anyone else who might need this:

    <?php 
    $author_id = get_the_author_meta('ID');// Needed because we are outside of the Loop
    $image = get_field('author_profile_picture', 'user_'. $author_id );
    
    if ( !( $image ) ) {
     echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'quark_author_bio_avatar_size', 125 ) ); // Defaults to Gravatar if no image is uploaded, the 'quark' filters is a theme function that sets the size, if not using Quark Theme you don't need that bit
    
    } else { ?>
      <img src="<?php echo $image; ?>" width="124" height="148" alt="<?php echo $author_id->nicename; ?>" />
    <?php } ?>

    My problem was that I wasn’t specifying the author by ID in order for my template to get the right image…..once I figured that out it worked.

    You CAN still use wp_get_attachment_image( $imgID, $size ); and that method works well, it’s just that it uses some core function to add classes and styling and image sizes – great if you don’t have more specific needs.

  • Thank you for being understanding……I really do try to work things out pretty exhaustively before I post for help…..I just hate asking for help until I really need it, but you’re right – sometimes I should just step away for a bit and work on something different, this was a good reminder. šŸ™‚

    I’m going to mark your last post as ‘this solved..” because it was your request for me to post my code that had me looking at it again…

  • Gah!!…..now it’s working. Sigh. I *swear* it was not working earlier, LOL. But I have been fiddling with this all day today….changing the setting from Array to URL to ID and back again, changing my code calling the image, etc. Maybe it’s been too long of a day.

    Very sorry for wasting your time but I really do appreciate your willingness to help, many thanks.

  • Hi John,
    Thanks for the quick reply. I did use that page as a resource, and tried all the methods listed – the only one that works is wp_get_attachment_image.

    The other methods, using basic object, customized display object, and URL, did not work.

    Also, in the current version of ACF Pro, the choices now are “Image Array”, “Image URL” or “Image ID” there is no longer an “Image Object” choice when creating a field.

    SO if I select “Image Array” and use the Object methods (basic or customized) it doesn’t display any image, it doesn’t recognize that there is a value stored at all in fact (my fallback Gravatar displays instead since I have an if(!$image) statement in case no image is uploaded).

    If I select URL and use the URL method, instead of echoing the URL in the image tag, it echos the ID number, so no image displays.

  • Well, I have it working, but not the way I’d like it to, and there is still a bug in ACF Pro (current version).

    The only way I can get it to work is to change how the image is stored to as an ID.

    The bug is that regardless of how I select it to be stored (my preference would be as a URL), it stores it as an ID anyway – so changing it to Array or URL has no effect – it still just shows up in the database with an ID number.

    BUT regardless of how it is stored, unless I change that setting to *also* be “ID” then I can’t use it using the get_field function.

    The ONLY way I can get it to work is to have it set to store as an ID and use the echo wp_get_attachment_image( $imgID, $size ); method, which is not ideal because then WP adds classes and styling that I don’t want and have to override with my own styles, and I am limited to the WP image sizes (although I know I can add my own and have, but in some places I just want to add a one-off image size without having to put it in my functions first) .

    If I could just echo the URL, I can structure my own image link using my own sizes and styles and pull in whatever other author meta I want….

    Any help would still be welcomed, ideally I’d like to see the bug fixed in a future update – if the option exists to store images as an array or as a URL, it should save to the database using the selected option.

  • EUREKA! John, you are a genius. I think if I had just removed the global $post in the very beginning it would have worked fine – when I put the code back to the way it was before (and exactly as you suggested) and removed global $post, it is working perfectly.

    Thank you a huge amount! If this forum had the emoji for a hug, I would include it here. šŸ™‚

  • Thanks John, I am actually laughing now, because that is how I started out…..and it made everything wonky…..I was getting the Deals post titles in place of the (applicable) Resort name, and lots of other things out of place. When I had wp_reset_postdata in place, everythiing after the first post was blank….oddly it showed the post div, with my HTML in place, but all the meta data was missing….

    BUT I will try it again, at this point I will try anything! AND it’s entirely possible that I may have originally had something tiny out of place – I just assumed because I was refererring to new posts, the global $post was confusing things and I had to change the second instance of $post to something else ($mypost, whatever, I just chose $relatedpost) ….maybe removing global $post will work, in the past I’ve needed it to access the custom metadata, which the standard WP loop doesn’t (or didn’t) make available.

  • Thanks John, I appreciate your continued help.

    I’m actually not modifying the Loop per se, it’s a search result page so the ‘loop’ is standard, and on the template it starts out with the usual (if(have_posts()) : while (have_posts()) : the_post();)

    Then below that I have the code that sets up all the custom field metadata, some from the current post and some from the ‘related’ (via a relationship field) post – this code is:

    global $post;     
    $bookingstart = get_field('booking_date_start');
    $bookingend = get_field('booking_date_end');
    $travelstart = get_field('travel_date_start');
    $travelend = get_field('travel_date_end');
    $promocode = get_field('booking_promo_code');
    
    $relatedposts = get_field('applicable_resort',$post->ID);
    if( $relatedposts ) { 
      foreach( $relatedposts as $relatedpost):  setup_postdata($relatedpost);
        $resortname = get_the_title($relatedpost->ID);
        $location = get_post_meta($relatedpost->ID,'location',true);
        $logo =  get_field('logo'); 
        $demoterms = get_the_terms($relatedpost->ID, 'demographic_category');
          if ( !is_wp_error($demoterms)) {
           $demographic_category_nolinks = wp_list_pluck($demoterms, 'name'); 
           $demographic_category_list = implode(", ", $demographic_category_nolinks);
          }
      endforeach;
      }

    The reasoning behind all this is that we have two CPTs – “Deals” (‘deal’) and “Resorts” (‘resort’)……we started out with just Deals, but it quickly became apparent it was too cumbersome to keep entering what it resort-specific information into each deal, so we created the second CPT for Resorts to hold all that information that is specific to the resort, then we relate the Deal to the Resort.

    It’s all worked great everywhere except for on the search results pages, and only on this one field – the logo – that I can’t seem to get the URL for it to echo into the image tag so it can display – it works fine on other pages, just not on the search results page.

    What seems odd to me is that when I look at my wp_postmeta table in phpMyAdmin, I see the metakey (‘logo’) but there is no value….however in it’s corresponding _metakey (‘_logo’) there is a value but it’s not a URL, so I’m not sure how to use it – it is the word field followed by a number.

  • Hi John – thank you so much for the quick reply – I accidentally clicked on “this solved my problem” when I was trying to click on the slider to read to the right (the page jumped on me at the last second!)….

    Sadly this did not actually work and I don’t know mark this as “unsolved” still……

    I tried both the method you have (get_field(‘applicable_resort’, $post-ID)); as well as the standard get_post_meta, neither worked. Still no image URL.

    I’ve been looking at wp_get_attachment_image function but not sure how to use it.

    How can I mark this as unsresolved?

  • Thanks @James!

    That worked perfectly, and thank you for the link – I do try to learn more all the time, but for some reason SQL queries still make me a bit nervous, although I do always backup my DB first.

    Your help is much appreciated šŸ™‚

Viewing 25 posts - 1 through 25 (of 39 total)