0

i need your help in making SQL

What i have:

  1. custom post type ( company )
  2. taxonomie ( category ) applyed to cpt ( and has ct section select field )
  3. custom taxonomie ( section ) applyed to nothing

In taxonomie ( category ) i set value on custom field ( using acf plugin ) of custom taxonomie ( section ). and it stores in database like these: in wp_options, key: {taxonomie}_{term_id}_{field_key}, value: section_term_id. For ex. i have category named "Wedding flowers" with id of 15 and tax. section term: "Weding" with id 14. So if i do: get_option( 'category_15_section' ) i'll get 14 ( _section is field key, it could be _aaa or othere, but i for shore know that it will be _section )

So, what i need is when i'm on archive page of cpt and i have $_GET['section'] variable ( wich is eaqual to section term id == 14 ) I need to echo all posts in taxonomie ( category ), and that category must have value of custom fied wich has value in options table set up like $_GET['section'].

Hope u understand what i need, if not ask some clearifying moments in comments.

my code so far:

add_filter( 'pre_get_posts', function( $query ) {
    if( ! is_main_query() || ! is_post_type_archive() || ! $query->get( 'section', false ) )
        return $query;

    $section = $query->get( 'section' );

    unset( $query->query['section'] );
    unset( $query->query_vars['section'] );
    $query->tax_query = false;
    $query->set( 'tax_query', false );

    add_filter( 'posts_where' , 'm7_24_section_posts_where' );

    //echo '<pre>'; var_dump( $query ); echo '</pre>';
    return $query;
} );
function m7_24_section_posts_where( $where ) {
    global $wpdb;
    remove_filter( current_filter(), __FUNCTION__ );

    $section = (int) $_GET['section'];
    //echo '<pre>'; var_dump( $where ); echo '</pre>';

    // here i need to make sql to add categories wich have value in options table
    // set up like $section
    return $where;
}

1 Answer 1

0

ok folks, i did it anothere way.. here's how i did it:

add_filter( 'pre_get_posts', function( $query ) {
    if( ! is_main_query() || ! is_post_type_archive() || ! $query->get( 'section', false ) )
        return $query;
    global $wpdb;

    $section = $query->get( 'section' );

    unset( $query->query['section'] );
    unset( $query->query_vars['section'] );
    $query->tax_query = false;
    $query->set( 'tax_query', false );

    $cateroties = $wpdb->get_results( $wpdb->prepare(
        "
        SELECT option_name, option_value
        FROM $wpdb->options
        WHERE option_name LIKE '%s'
        ",
        'category_%_section'
    ) );

    $category__in = array();
    foreach( $cateroties as $category ) {
        $cat_id = $category->option_name;
        $values = unserialize( $category->option_value );
        if( in_array( $section, $values ) ) {
            $category__in[] = str_replace( 'category_', '', str_replace( '_section', '', $cat_id ) );
        }
        //
    }

    $query->set( 'category__in', $category__in );

    return $query;
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.