Timur
Timur

Reputation: 23

Can't get_posts() custom by category or tag

I try to get_posts by category, get the posts (slides) which have only one tag or category. In my Wordpress theme i have parallax-slider, it uses a custom post type slider, and do this query:

$args = array(     
    'post_type'        => 'slider',
    'posts_per_page'   => -1,
    'post_status'      => 'publish',
    'orderby'          => $orderby,
    'order'            => $order,
    'suppress_filters' => $suppress_filters
);
$slides = get_posts( $args );

this way i get all post by this post type. after this data go to cycle:

foreach( $slides as $k => $slide ) {



            $url                = get_post_meta($slide->ID, 'my_slider_url', true);
            $thumb_url          = wp_get_attachment_image_src( get_post_thumbnail_id($slide->ID), 'slider-thumb');
            $sl_image_url       = wp_get_attachment_image_src( get_post_thumbnail_id($slide->ID), 'full');
            $caption            = get_post_meta($slide->ID, 'my_slider_caption', true);

I create taxonomy of tags and category:

function my_post_type_slider() {
    register_post_type( 'slider',
        array(
            'label'               => theme_locals("slides"),
            'singular_label'      => theme_locals("slides"),
            '_builtin'            => false,
            'exclude_from_search' => true, // Exclude from Search Results
            'capability_type'     => 'page',
            'public'              => true,
            'show_ui'             => true,
            'show_in_nav_menus'   => false,
            'taxonomies'          => 'slider_tag',
            'rewrite' => array(
                            'slug'       => 'slide-view',
                            'with_front' => FALSE,
                        ),
            'query_var' => 'slider', // This goes to the WP_Query schema
            'menu_icon' => ( version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) ) ? 'dashicons-slides' : PARENT_URL . '/includes/images/icon_slides.png',
            'supports'  => array(
                                'title',
                                'thumbnail',
                                'page-attributes',
                            )
        )
    );

    register_taxonomy(
        'slider_category',
        'slider',
        array(
            'hierarchical'  => true,
            'label'         => theme_locals("categories"),
            'singular_name' => theme_locals("category"),
            'rewrite'       => true,
            'query_var'     => true
        )
   );


    register_taxonomy(
        'slider_tag',
        'slider',
        array(
            'hierarchical'  => false,
            'label'         => theme_locals("tags"),
            'singular_name' => theme_locals("tag"),
            'rewrite'       => true,
            'query_var'     => true
        )
    );
}

add_action('init', 'my_post_type_slider');'

About taxonomy, I can view and work with it in admin panel - I was check it this way:

$category = get_terms( 'slider_category', $args ); print_r ($tags); 

and I can get:

Array ( 
    [0] => WP_Term Object ( 
        [term_id] => 46 
        [name] => 123 
        [slug] => 123 
        [term_group] => 0 
        [term_taxonomy_id] => 46 
        [taxonomy] => slider_category 
        [description] => 
        [parent] => 0 
        [count] => 3 
        [filter] => raw 
    ) 
)

But then I try to use it in get_posts with args (category_name=>'nameofcat' or tag=>'tagname') or another options I get empty $slides... I also try to use 'tax-query' and still can't get any result...

 $args = array( 'tax_query'     => array(
    array(
        'taxonomy'  => 'slider_tag',
        'field'     => 'names',
        'terms'     => $slug_name,
    ),),
'posts_per_page'   => -1,
'post_status'      => 'publish',
'orderby'          => $orderby,
'order'            => $order,
'suppress_filters' => $suppress_filters
                                         );

Can I get some slides data by cat/tag using get_posts? I know I can use global WP_query but this also no good works, because I need re-write cycle.

Upvotes: 1

Views: 1238

Answers (1)

Timur
Timur

Reputation: 23

I had wrong query $args of tax_query.

Here is normal code:

$args = array(
    'numberposts' => -1,
    'post_type'   => 'slider',
    'exclude'     => $the_id,
    'tax_query'   => array( 
        array(
            'taxonomy' => 'slider_tag',
            'field'    => 'slug',
            'terms'    => $slug_name,
        )
    )
);  
$slides = get_posts( $args );

Upvotes: 1

Related Questions