Support » Developing with WordPress » Define a variable to custom post

  • Resolved mroobov

    (@mroobov)


    Hello all.

    I’m more a designer than a coder, and i reach my limits with my problem.

    I’ve created a custom post type (with Toolset Types) which has a custom taxonomy (created with Types also) and each element of that taxonomy has a numeric custom field (created by Types again).

    That numeric field is a note, from 0 to 5.

    I would like to define a variable (probably in functions.php) for each post of my custom type wich is the addition of all numeric values of my custom field.

    An example to make it clear.

    I create “My post” from my “custom_post”
    I give it 3 taxonomy terms from my custom taxonomy list, with custom numeric field (between commas) :
    Bluetooth (4)
    Wifi (5)
    USB (3)

    (All that part is ok and done with Toolset Types)

    But i would like to define a variable which is the addition of 4+5+3 so 12 for this post above. And be able to call it from different plugins to sort my posts with that variable.

    It is probably a little function to put in functions.php, but i’m not a coder, and i really can’t find it online.

    Thank you for your time.

Viewing 3 replies - 1 through 3 (of 3 total)
  • From now, if someone is reading this, i’ve made a function i call on each single page, it calculates and returns me a number. The score of the actual post.

    From this point i would like to know in wich direction i have to search to be able to sort all my posts from that ‘score’ variable.

    Do i have to define the variable somewhere ?

    What i would like to tell WordPress is “Ok, all my posts have a score, you can get the score of each post with my_score_function(), now order alls post by ‘score’ DESC”

    • This reply was modified 3 weeks, 5 days ago by  mroobov.

    the direction you could start is to do a custom query where you could set the parameters you are wanting: this is just an example.

    $args = array( 
        'post_type' => 'toolset_types',
         'tax_query' => array(
            array (
                'taxonomy' => 'numeric_field',
                'field' => 'slug',
                'terms' => 'score',
            ),
          'order_by' => 'DESC
        ),
       );
       $loop = new WP_Query( $args );
    
       while ( $loop->have_posts() ) : $loop->the_post();
         your html content to display the data
       endwhile; 

    something similar to that

    • This reply was modified 3 weeks, 5 days ago by  dxladner.
    • This reply was modified 3 weeks, 5 days ago by  bcworkz. Reason: code fixed, 1st backtick must be in col 0

    Thank you for your answer.

    But i anwser to myself.

    What i needed at this point, is to manually add a meta to the posts.
    (this process is quite easy to find online)
    Anytime i create or edit a new post, i launch my function() and store the result in the meta when the post is saved.

    Now the result is available in the database, it can be used with any plugin or whatever.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.