• Resolved ihosely

    (@ihosely)


    Hi

    I am trying to use pre_get_posts to filter posts based on CFS field values.

    – I have posts with boolean field named _is_a_video
    – I want to use pre_get_posts to limit posts only to the ones with TRUE value for _is_a_video field

    The problem is I don’t know how to access the field value in the function. How to make the following code works?

    function limit_to_video_posts($query) {
        if ($query->is_author() AND $query->is_main_query()) {
            $query->set(.....);
        }
    }
    
    add_action('pre_get_posts', 'limit_to_video_posts');

    https://wordpress.org/plugins/custom-field-suite/

Viewing 1 replies (of 1 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    You don’t really need pre_get_posts to filter by custom field value. You could just use meta_query within WP_Query.

    E.g.

    $args = array(
      'post_type' => 'post',
      'meta_query' => array(
        array(
            'meta_key' => '_is_a_video',
            'meta_value' => 1,
        )
      )
    );
    $query = new WP_Query( $args );
    
Viewing 1 replies (of 1 total)
  • The topic ‘How to access fields values in functions.php’ is closed to new replies.