Title: Adding general custom fields
Last modified: July 24, 2018

---

# Adding general custom fields

 *  [dacevid](https://wordpress.org/support/users/dacevid/)
 * (@dacevid)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/adding-general-custom-fields/)
 * I am currently working on a webpage that show a slider in the homepage. To know
   which posts it should display there, it looks for posts marked as “featured” (
   which is a custom field) and it show the X more recent (where X is a number I
   set in code).
 *     ```
       $args = array(
           'post_type' => 'post',
           'posts_per_page' => 10,
           'meta_query' => array(
             array(
               'key' => 'sfeatured',
               'value' => 'super-sfeatured',
               'compare' => '='
             )
           ),
           'orderby'=>'date',
           'order'=>'DESC'
         );
         $the_query = new WP_Query( $args );
       ```
   
 * I don’t like having to change the amount of posts shown in the slider from the
   code (now the amount is 10, as you see). I would like to have a “general” custom
   field in my WordPress admin page where I can set the number of slides I want 
   to show.
 * What is the best way to approach this?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/adding-general-custom-fields/#post-10523006)
 * I’d suggest using the [Customize API](https://developer.wordpress.org/themes/customize-api/)
   to add a section and field to the Customizer for controlling this.
 * So for example, you could add a Section for the Slider:
 *     ```
       $wp_customize->add_section( 'my_slider', array(
       	'title'    => 'Slider',
       	'priority' => 160,
       ) );
       ```
   
 * Then a Setting and Control for number of slides:
 *     ```
       $wp_customize->add_setting( 'my_slider_number' );
   
       $wp_customize->add_control( 'my_slider_number', array(
       	'type'    => 'number',
       	'section' => 'my_slider',
       	'label'   => 'Number of slides',
       ) );
       ```
   
 * Then you can use the saved value with [`get_theme_mod()`](https://developer.wordpress.org/reference/functions/get_theme_mod/):
 *     ```
       $args = array(
       	'post_type'      => 'post',
       	'posts_per_page' => get_theme_mod( 'my_slider_number', 10 ),
       	'orderby'        => 'date',
       	'order'          => 'DESC'
       	'meta_query'     => array(
       		array(
       			'key'     => 'sfeatured',
       			'value'   => 'super-sfeatured',
       			'compare' => '='
       		)
       	),
       );
       $the_query = new WP_Query( $args );
       ```
   
    -  This reply was modified 7 years, 10 months ago by [Jacob Peattie](https://wordpress.org/support/users/jakept/).
 *  Thread Starter [dacevid](https://wordpress.org/support/users/dacevid/)
 * (@dacevid)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/adding-general-custom-fields/#post-10523018)
 * Thank you for your answer [@jakept](https://wordpress.org/support/users/jakept/)
 * I’ve had another idea but it doesn’t seem to work even if it should. Could you
   help me?
 * as I am using ACF plugin, I have created a new custom field named ‘slides_number’,
   and I have added to one of my pages (the one with ID=194). Then in my view I 
   do this:
 *     ```
       $slides_number = get_field('slides_number', 194);
   
             $args = array(
               'post_type' => 'post',
               'posts_per_page' => $slides_number,
               'meta_query' => array(
                 array(
                   'key' => 'sfeatured',
                   'value' => 'super-sfeatured',
                   'compare' => '='
                 )
               ),
               'orderby'=>'date',
               'order'=>'DESC'
             );
             $the_query = new WP_Query( $args );
       ```
   
 * But it doesn’t seem to get the value. What am I missing?
    -  This reply was modified 7 years, 10 months ago by [dacevid](https://wordpress.org/support/users/dacevid/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/adding-general-custom-fields/#post-10530796)
 * Are you sure $slides_number has no value? Or could it be that it has no effect
   in the query? This can happen when queries are overridden in the “pre_get_posts”
   action.
 * Try adding this line after getting the field:
    `echo "Number of slides: $slides_number
   <br>\n";`
 * Upload the edited template, then reload the page. The output should appear on
   the page somewhere, including the field value. If there is no field value, there
   is a problem with ACF functionality. If there is a value but the slides returned
   count does not match, there is likely interfering code added through “pre_get_posts”
   action.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Adding general custom fields’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [general](https://wordpress.org/support/topic-tag/general/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 10 months ago](https://wordpress.org/support/topic/adding-general-custom-fields/#post-10530796)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
