Support

Account

Forum Replies Created

  • Hi everybody,
    I found the solution.
    Instead of searching like me for hours I share it if someone has this issue too.
    You just have to choose post ID instead of post object and it works like a charm.
    Best.

  • My code works with current events.

    $myfield= get_field('myfield');
    $myfield_ID = $myfield->ID;
    echo $myfield_ID;
    

    It returns event ID with current events but with past events it returns me nothing, none ID…
    With ID, I can display event linked by acf post object title, thumbnail, etc… but without, impossible to display anything from a past event.
    If I replace manually the ID variable (ex: $myfield_ID = ‘4055’;) it displays the past event.
    I dont understand why it doesn’t returns me the ID, even if it’s a past event.
    Any help appreciated.
    Best.

  • Hello @beapi,
    I have the same issue and your previous code didn’t work anymore.
    Do you have an updated code ?
    Thanks in advance.
    Best.

  • Hello @hube2,
    I followed your advice and I did a query directly inside my archive template with your 3rd choice.
    And it works like a charm.
    You’re the best.
    Thank you so much !!
    All the best.
    Pierre

  • @hube2,
    Many thanks !!
    This third solution seems to be very cool.
    I need to do it as a “pre_get_posts” function with my $taxquery so as to hide some custom taxonomies.
    I never did a loop inside a function inside my functions.php, the loop is inside my archives-films.php.
    I have to test this, I will do it tomorrow and keep you in touch.
    Thanks for all.
    Best.
    Pierre

  • Ok, I think the second choice seems to the better solution.
    What do you think ?

  • Hello @hube2,
    Thanks again for taking time for me and my issue…
    I’m sorry, I think I’m not explaining myself well.
    I would like to display first the posts (CPT : films) that have a date (acf date picker field : date_de_sortie) and then those that do not.
    Currently they display first those without a date.
    I made a photomontage to explain what I’m looking for (I framed date on it to see where date is).
    Here is the photomontage
    All the best.
    Pierre

  • I tried again today, I didn’t reach to do what I’m looking for.
    Maybe I need a new approach, any thoughts ?
    Best

  • @hube2 yes it’s exact, the date field exist but is empty on some posts.
    I have updated to :

    
        $query->set( 'meta_query', array(
          'relation' => 'AND',
          'date_clause' => array(
            'key' => 'date_de_sortie',
            'value' => '',
            'compare' => '!=',
            )
          )
        );
    

    Now it displays only posts with date field not empty.
    Posts whith empty date field are not displayed.

  • Me neither… I don’t understand.
    Yes my code is running.
    I tried running it with :

    
    //$query->set( 'tax_query', $taxquery );
    

    It doesn’t interfer.
    The field ‘date_de_sortie’ is my ACF date picker field.
    If I set ‘date_clause’ => ‘DESC’, it effectively sorts posts in the opposite direction which is logical, but always posts without any ‘date_de_sortie’ are in first.
    Hope I’m clear in my explanations ;-).
    Best.
    Pierre

  • Hello @hube2
    Ok, oh yes I understand.
    I tried with “date_clause” and unfortunately it’s still the same…
    Here is the updated code :

    
    add_action( 'pre_get_posts', 'films_query' );
    function films_query( $query ) {
      if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
        $cat_hide = get_field('categorie_des_films_a_masquer', 'options');
        $taxquery = array(
          array(
            'taxonomy' => 'categorie_films',
            'field' => 'term_id',
            'terms'    => $cat_hide,
            'operator' => 'NOT IN',
          )
        );
        $query->set( 'posts_per_page', '-1' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'tax_query', $taxquery );
        $query->set( 'meta_query', array(
          'relation' => 'AND',
          'date_clause' => array(
            'key' => 'date_de_sortie',
            'compare' => 'EXISTS',
            )
          )
        );
        $query->set( 'orderby', array(
          'date_clause' => 'ASC',
          'title'   => 'ASC',
          )
        );
      }
    }
    
  • Hello everybody,
    sorry to be pushy, any idea how to solve it ?
    Thank you in advance.
    Best.
    Pierre

  • Here is the new code with meta_query, but same result.
    Any idea on what I’m missing ?

    
    add_action( 'pre_get_posts', 'films_query' );
    function films_query( $query ) {
      if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
        $cat_hide = get_field('categorie_des_films_a_masquer', 'options');
        $taxquery = array(
          array(
            'taxonomy' => 'categorie_films',
            'field' => 'term_id',
            'terms'    => $cat_hide,
            'operator' => 'NOT IN',
          )
        );
        $query->set( 'posts_per_page', '-1' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'tax_query', $taxquery );
        $query->set( 'meta_query', array(
          'relation' => 'AND',
          'date' => array(
            'key' => 'date_de_sortie',
            'compare' => 'EXISTS',
            )
          )
        );
        $query->set( 'orderby', array(
          'date' => 'ASC',
          'title'   => 'ASC',
          )
        );
      }
    }
    
  • Here is the new code with meta_query but same results.
    There is something I don’t understand…

    
    add_action( 'pre_get_posts', 'films_query' );
    function films_query( $query ) {
      if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
        $cat_hide = get_field('categorie_des_films_a_masquer', 'options');
        $taxquery = array(
          array(
            'taxonomy' => 'categorie_films',
            'field' => 'term_id',
            'terms'    => $cat_hide,
            'operator' => 'NOT IN',
          )
        );
        $query->set( 'posts_per_page', '-1' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'tax_query', $taxquery );
        $query->set( 'meta_query', array(
          'relation' => 'AND',
          'date' => array(
            'key' => 'date_de_sortie',
            'compare' => 'EXISTS',
          )
        )
      );
      $query->set( 'orderby', array(
        'date' => 'ASC',
        'title'   => 'ASC',
      )
    );
    }
    }
    
  • Here is with meta_query args but it didn’t work, I have the same results.
    There is something I don’t understands.

    
    add_action( 'pre_get_posts', 'films_query' );
    function films_query( $query ) {
      if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
        $cat_hide = get_field('categorie_des_films_a_masquer', 'options');
        $taxquery = array(
          array(
            'taxonomy' => 'categorie_films',
            'field' => 'term_id',
            'terms'    => $cat_hide,
            'operator' => 'NOT IN',
          )
        );
    
        $query->set( 'posts_per_page', '-1' );
        $query->set( 'post_status', 'publish' );
    
        $query->set( 'tax_query', $taxquery );
        $query->set( 'meta_query', array(
          'relation' => 'AND',
          'date' => array(
            'key' => 'date_de_sortie',
            'compare' => 'EXISTS',
          )
        )
      );
      $query->set( 'orderby', array(
        'date' => 'ASC',
        'title'   => 'ASC',
      )
    );
    }
    }
    
  • Hello @hube2,
    Thanks for taking on your time to answer.
    I’ll have a look at the codex.
    I’ll keep you in touch.
    Thanks.
    Pierre

  • Thanks for your answer.
    My populate field has a unique ID but not linked to the row.
    I’ll dig with the links you gave me.

    Yes, you’re probably right that it would be better using another custom post type and doing relashionship between us.
    I’ll try it too.

    Many thanks for your time.

    All the best.

  • That’s where I’m blocked.
    I’m not able to identifying each row using Title populate field for exemple.

  • Hi @john,
    I’m reaching what I want to do but with custom taxonomies.
    It would be awesome to do the same thing with options, it’s user friendly with options.
    I will try to reach with options a bit later for the challenge ;-).
    Have a nice day.
    Best

  • I cannot create a relation ship from custom post to options.
    A custom post must be linked to only one subject and one location.
    A subject and a location can be linked to many posts.
    Best

  • No, I didn’t use relationship field at all.
    But maybe I should.
    Best

  • Hello @John,
    many thanks for your answer.
    I’m sorry, I didn’t explain well how I did and what are my goals.

    Here is a diagram of what I did.
    CTP and Options IMG

    The idea is to make relationship between custom posts and their own options fields.
    For example with three custom posts which display their own informations/values from options fields.

    CTP 1 -> repeater “Subject” row[1] -> Title, description, textfield, etc. and from the other repeater field “Location/Town” row[4].

    CTP 2 -> repeater “Subject” row[4] -> Title, description, textfield, etc. and from the other repeater field “Location/Town” row[1].

    CTP 3 -> repeater “Subject” row[2] -> Title, description, textfield, etc. and from the other repeater field “Location/Town” row[2].

    I delete Group field which was unecessary.
    I populate title and town from options to my custom post two dedicated radio fields.

    Now I’m looking for displaying on the frontend values from options fields that “belong to my custon post”.
    Populate not do relationship between CPT and Options.
    I tought that with post_id, I’ll be able to display options values like I can do with taxonomies.
    Am I wrong ?

    I hope it’s more clear…

    Let me know.

    All the best.

    Pierre

  • Hi again,
    I’m looking to my database and I don’t understand how to find a link by post_id, meta_id and option_id
    Any help appreciated.
    All the best.

  • Maybe, I’m doing wrong with a populated radio button field.
    What do you think of this solution ?
    https://www.advancedcustomfields.com/resources/bidirectional-relationships/
    Best

  • Hello epoka,
    here is my php code to register my custom post type I used.
    Even if my function name is cptui_register_my_cpts, I started from scratch a new php code:
    Strings are in french ;-).

    function cptui_register_my_cpts() {
    
    	$labels = array(
    		"name" => __( 'Adhérents', '' ),
    		"singular_name" => __( 'Adhérent', '' ),
    		"menu_name" => __( 'Adhérents', '' ),
    		"all_items" => __( 'Tous les adhérents', '' ),
    		"add_new" => __( 'Ajouter un adhérent', '' ),
    		"add_new_item" => __( 'Ajouter un nouvel adhérent', '' ),
    		"edit_item" => __( 'Éditer un adhérent', '' ),
    		"new_item" => __( 'Nouvel adhérent', '' ),
    		"view_item" => __( 'Voir adhérent', '' ),
    		"view_items" => __( 'Voir les adhérents', '' ),
    		"search_items" => __( 'Rechercher un adhérent', '' ),
    		"not_found" => __( 'Aucun adhérent trouvé', '' ),
    		"not_found_in_trash" => __( 'Aucun adhérent trouvé dans la corbeille', '' ),
    		"featured_image" => __( 'Image à la une', '' ),
    		"set_featured_image" => __( 'Choisir image à la une', '' ),
    		"remove_featured_image" => __( 'Supprimer image à la une', '' ),
    		"use_featured_image" => __( 'Utiliser comme image à la une', '' ),
    	);
    
    	$args = array(
    		"label" => __( 'Adhérents', '' ),
    		"labels" => $labels,
    		"description" => "Nos adhérents",
    		"public" => true,
    		"publicly_queryable" => true,
    		"show_ui" => true,
    		"show_in_rest" => false,
    		"rest_base" => "",
    		"has_archive" => "adherents",
    		"show_in_menu" => true,
    		"exclude_from_search" => false,
    		"capability_type" => "post",
    		"map_meta_cap" => true,
    		"hierarchical" => false,
    		"rewrite" => array( "slug" => "adherents", "with_front" => false ),
    		"query_var" => true,
    		"supports" => array( "title", "editor", "thumbnail" ),
    		'menu_position' => 20,
        "menu_icon"	=> "dashicons-groups",
    	);
    
    	register_post_type( "adherents", $args );
    	
    }
    
    add_action( 'init', 'cptui_register_my_cpts' );

    I really hope it will work for you.
    Keep me in touch.
    All the best.
    Pierre

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