htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.37k stars 245 forks source link

Usage does not work: "->where( 'post_level', '=', 2 )" and "->where( 'post_parent_id', '=', 12 )" #1197

Open valexxas opened 10 months ago

valexxas commented 10 months ago

Version

Expected Behavior

Output meta fields only on child pages for the specified specific page

Actual Behavior

The nesting level and parent page are not defined. If the condition is ->where( 'post_parent_id', '=', 0 ), then the fields will be displayed on all pages, including on child pages (having a parent page). Also, when using ->where( 'post_level', '=', 0 ), the field is always output, including on child pages. When specifying ->where( 'post_level', '=', <ANY_NUMBER>) or ->where( 'post_parent_id', '=', <ID_PARENT_PAGE> ) the fields are not output anywhere.

Container definition

add_action( 'carbon_fields_register_fields', function () {
    Container::make( 'post_meta', __( 'Meta fields' ) )
        //->where( 'post_type', '=', 'page' )
        //->where( 'post_level', '=', 2  )
        ->where( 'post_parent_id', '=', 15 )
        ->add_fields( array(
        Field::make( 'text', 'page_title', 'Page Title' )
            ,
        ) )
    ;
} );

Comments

I'm sorry, I'm using a translator to communicate with you. There may be problems with the translation of phrases.

valexxas commented 10 months ago

I will supplement it. Conditions: child page, condition for fields "->where( 'post_level', '=', 1 )" ( only the first level, not child ones), fields should not be output. What happens: the CarbonFields fields are output, the input values are not saved. On the pages of the first level, the fields are displayed, their values are saved. Removing the condition "->where( 'post_level', '=', 1 )". Result: fields are displayed everywhere, all values are saved as they should be.

valexxas commented 5 months ago

After experimentation, the problem was circumvented by a similar condition:

Container::make( 'post_meta',__( 'Meta fields' ) )
  ->where( 'post_type', '=', 'page' )
  ->where( 'post_level', 'CUSTOM', function( $post_level ) {
  return ( $post_level == 2 );
   } )
// OR
  ->where( 'post_parent_id', 'CUSTOM', function( $post_parent_id ) {
    return ( $post_parent_id == wc_get_page_id( 'shop' ) );
  } )