Viewing 5 replies - 1 through 5 (of 5 total)
  • You don’t need to save it as a nubmer. Just use it as a number when you do your processing.

    As a note, both PHP and MySQL can use text values as numbers. for PHP it’s as easy as just declaring it, and if you are unsure you can use either intval() or floatval() to force it to a numeric value. For MySQL you can use CONVERT() to cast the value as a nubmer to perform any calculations that you need.

    Thread Starter brsastre

    (@brsastre)

    Hi catacaustic,

    I knew the intval(). The thing is I am performing a wp_query, and I need the posts to be ordered by a number added with a custom metabox. How can I perform intval() within the $args?

    // Wp Query
                                $args = array(
                                    'post_type' => 'articles',
                                    'orderby' => 'meta_value',
                                    'meta_key' => '_article_number',
                                );

    “…for PHP it’s as easy as just declaring it…”
    I didn’t knew this. How ?

    I don’t think that you can do it that way. The WordPress queries work with text, which will order as a string. You’d need to do this a custom query, which isn’t too hard if you know a bit of SQL.

    As a very quick (and not even close to tested) query example that ha sa good chance of not doing exactly what you want it to…

    global $wpdb;
    
    $query = $wpdb->prepare ("SELECT ID
    FROM ".$wpdb->posts." AS p
    WHERE p.post_type = 'articles'
        AND post_status = 'publish'
    INNER JOIN ".$wpdb->postmeta." AS pm
        ON pm.post_id = p.ID
        AND pm.meta_key = '_article_number'")
    ORDER BY CAST(pm.meta_value AS INT) ASC;

    “…for PHP it’s as easy as just declaring it…”
    I didn’t knew this. How ?

    This only applies if you’re uisng the value in PHP, not in anything outside of it.

    Thread Starter brsastre

    (@brsastre)

    Catacaustic, thank you very much. I will try that.
    You are awesome 🙂

    Thread Starter brsastre

    (@brsastre)

    May be I didn’t explain well enough.

    I need to query all my articles that belong to the same magazine (let’s say number 1)
    that belong to a specific custom post type (articles) and are publish.
    And I really need to order by article number. So I have two custom meta_values:
    _magazine-number for the filtering, and _article-number for the ordering.

    this is the closest I could get:

    SELECT pm.post_id
    FROM wp_yxgi_postmeta as pm
    INNER JOIN wp_yxgi_posts p
    ON p.ID = pm.post_id
    WHERE pm.meta_key = '_magazine-number' AND pm.meta_value = '1'
    AND p.post_status = 'publish'
    AND p.post_type = 'articulos'

    The thing is, how can I order by ‘_article-number’ If I have already told the query that ‘meta_key’ is ‘__magazine-number’ ?

    I have been dealing with this the whole day!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘metabox value as number’ is closed to new replies.