WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problems when filtering posts by category, ordering them by custom field (3 posts)

  1. Figotech
    Member
    Posted 3 years ago #

    The idea is pretty simple: I have different sections that must be shown on the front page with their own posts, and these posts must be in a very specific order.

    It is a sort of online-magazine so each post belongs to 2 categories, the publication number and the type of article it is.

    the queries used are a variation of the next code snippet:

    query_posts(array('category__and' => array(14,$currentnumber)));

    Where I assign $currentnumber before to reuse the page for every different publication number.

    The problem appeared when I tried to order the articles using a custom field. I named it "Art_Order" and assigned a value of 1 to 5 in one section, 1 to 5 in another. This is how the query looked:

    query_posts(array('category__and' => array(14,$currentnumber)).'&meta_key=Art_Order&orderby=meta_value_num&order=ASC');

    The ordering works, but the problem is the filtering is screwed, since all articles no matter what category they belong to contain a "Art_Order" custom field with a value, so it includes all posts in the same section.

    My next solution was to create a different field per section (Art_order_articletype1 and so on..) Works like a charm, except I now mix articles from different publication numbers!!! I can't create a specific field for each article type + publication number, it would be way too much work.

    Is there a way to make it filter by the categories and use the meta key to know which field used to order but to avoid including the meta key posts???

    Additional background info: I have the same text in the content and excerpt fields: so i can put a number in the excerpt but I have tried a "orderby=excerpt" in my query and doesn't work. Is there a way to order by excerpt content???

    Thanks

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    The weird behaviour with your query is due to how your parameters have been written.

    array('category__and' => array(14,$currentnumber)).'&meta_key=Art_Order&orderby=meta_value_num&order=ASC'

    The code above is stringing together an array and a string (which you can't really do in php - it's either a string, or an array, it can't be both at once).

    Switch to using an array for all the args...

    $args = array(
    	'category__and' => array( 14, $currentnumber ),
    	'meta_key' => 'Art_Order',
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC'
    );
    query_posts( $args );

    Hope that helps... :)

  3. Figotech
    Member
    Posted 3 years ago #

    Worked perfectly!!! Thank you very much

Topic Closed

This topic has been closed to new replies.

About this Topic