WordPress.org

Forums

WordPress bug? custom_post and WP_Query using category (1 post)

  1. aliwebdesarrollo
    Member
    Posted 1 year ago #

    Hello,

    I'm trying to display custom post beside the normal loop.

    Here is what i have until now:

    Custom Post type:

    register_post_type( 'publicidad',
    	array(
    		'labels' => array(
    			'name' => __( 'Publicidad' ),
    			'singular_name' => __( 'Publicidad' )
    		),
    		'public' => true,
    		'has_archive' => true,
    		'show_in_menu' => true,
    		'show_ui' => true,
        		'query_var' => true,
        		'rewrite' => true,
        		'capability_type' => 'post',
        		'has_archive' => true,
        		'hierarchical' => false,
        		'menu_position' => null,
        		'show_admin_column'=>true,
        		'supports' => array( 'title', 'thumbnail')
    		)
    	);

    This is my custom taxonomy for my custom post type "publicidad"

    $labels = array(
    		'name' => _x( 'Ubicaciones', 'taxonomy general name' ),
    		'singular_name' => _x( 'Ubicacion', 'taxonomy singular name' ),
    		'search_items' =>  __( 'Buscar Ubicacion' ),
    		'all_items' => __( 'Todos las Ubicacion' ),
    		'edit_item' => __( 'Editar' ),
    		'update_item' => __( 'Actualizar' ),
    		'add_new_item' => __( 'Agregar Nueva' ),
    		'new_item_name' => __( 'Nueva Ubicacion' ),
        ); 	
    
        register_taxonomy(
    		'ubicaciones','publicidad',array(
    			'hierarchical' => false,
    			'labels' => $labels,
    			'show_admin_column'=>true,
    			'show_in_menu' => true
    		)
        );

    Also register that this custom post type could use regular categories and tags

    register_taxonomy_for_object_type( 'category', 'publicidad' );
        register_taxonomy_for_object_type( 'post_tag', 'publicidad' );

    I create a new wp_query with this args

    $args_ads=array(
    			'post_type' => 'publicidad',
    			'post_status' => 'publish',
    			'category__in' => $wp_query->get('cat'),
    			'tax_query' => array(
    				array('taxonomy' => 'ubiaciones',
    				'field' => 'slug',
    				'terms' => 'ub-categoria'
    				)
    			),
    			'orderby' => 'rand'
    		);
    
        $query_ads = new WP_Query($args_ads);

    There is most of the problem the custom post are not showed. When i check the request in the new wp_query i found that this new query one of the args i set is not been taking in count or somehow is changing from "publicidad" to "post".

    Here is the SQL Query that shows me when i do a var_dump of $query_ads

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (74) AND tt1.term_taxonomy_id IN (8) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY RAND() DESC LIMIT 0, 15

    As you could see the post_type is set to post instead of publicidad

    I also try changing one of the args in $query_ads, from "category__in" to "publicidad__in" and then the query is this:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (74) ) AND wp_posts.post_type = 'publicidad' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY RAND() DESC LIMIT 0, 10

    What i get there is that WordPress accept the post_type i set, but the the post is showed in all categories, and i have to put into the code an special condition (in_category()) to see if the post goes there or not, the problem is that if i have 200 post where the "ubicacion" is "ub-categoria" it will bring me all of them and it will makes really slow the site.

    Could someone help me? if its my mistake? or if it is a wordpress bug?

Topic Closed

This topic has been closed to new replies.

About this Topic