0

I'm getting started with advanced custom fields. I have a category field (main_category) where the categories given below are selected using the radio buttons.

1. Electronics
2. Programming
3. Design

where there are sub categories for these categories. they are set using the conditional radio buttons for each category. the sub categories are indexed, the fields are given ion brackets,

Post Type: article
Field Name: Category
Field key : main_category

1. Electronics - - - - - - - - (electronics)
        Linear Electronics - -      (elect_linear)
        Computer Networks- - -      (elect_networks)
        Microwave Engineering-      (elect_microwave)
        MicroProcessors- - - -      (elect_micropro)
2. Programming - - - - - - - - (programming)
        ObjectiveC - - - - - -      (prog_objc)
        Pythion  - - - - - - -      (prog_python)
        JavaScript - - - - - -      (prog_js)
        PHP  - - - - - - - - -      (prog_php)
3. Design     - - - - - -  - - (design)
        Illustrator  - - - - -      (desgn_ai)
        Photoshop   - - - - - -     (desgn_ps)
        After Effects   - - - -     (desgn_ae)
        Cinema 4d   - - - - - -     (desgn_cf)

the WP_Query was made,

$articles = array(
  'numberposts' => -1,
  'post_type' => 'article',
  'meta_key' => 'main_category',
);


$query_articles = new WP_Query( $articles);

it returned an array $query_articles which contains all the posts., how can I filter through the array $query_articles ? so that I would add 'tagged in php' for the articles in the php sub category.?

what i could do in my knowledge was to query arrays with different categories..

$electronics = array(
  'numberposts' => -1,
  'post_type' => 'article',
  'meta_key' => 'main_category',
  'meta_value' => 'electronics'
);
$programming = array(
  'numberposts' => -1,
  'post_type' => 'article',
  'meta_key' => 'main_category',
  'meta_value' => 'programming'
);
$design = array(
  'numberposts' => -1,
  'post_type' => 'article',
  'meta_key' => 'main_category',
  'meta_value' => 'design'
);



//query variables
$query_electronics = new WP_Query( $electronics);
$query_programming = new WP_Query( $programming);
$query_design = new WP_Query( $design );

but the sub categories always messed up and also it eats up lots of resource.

ps. this is for a blog i'm developing in localhost. thanks for any help and comment.

1 Answer 1

2

If main_category is a category (custom taxonomy), then you're querying it wrong. This should go like this:

$electronics = array(
    'numberposts' => -1,
    'post_type' => 'article',
    'main_category' => 'electronics'
);
$query_electronics = new WP_Query( $electronics );

If that's a custom field, use meta_query:

$electronics = array(
    'numberposts' => -1,
    'post_type' => 'article',
    'meta_query' => array(
        'key' => 'main_category',
        'value' => 'electronics'
    ),
);
$query_electronics = new WP_Query( $electronics );
1
  • by the way how it comes to echo something like tagged in 'some category'?
    – x0x
    Commented Apr 25, 2014 at 21:28

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.