• I need to query all my articles (custom post type) that belong to the same magazine (another custom post type) let’s say, magazine number 1, and are publish.

    And I really need to order by article number (custom meta box). 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 2 replies - 1 through 2 (of 2 total)
  • Using WP_Query might be a quicker way than using a SQL Query,

    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Something like:

    $args = array(
    	'meta_key'   => '_magazine-number',
    	'meta_value' => '1',
            'post_type'  => 'page',
            'post_status' => 'publish'
    );
    $query = new WP_Query( $args );

    Then you’d use the $query variable in a loop.

    //The Argument
    $args = array(
    	'meta_key'   => '_magazine-number',
    	'meta_value' => '1',
            'post_type'  => 'page',
            'post_status' => 'publish'
    );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    	echo '<ul>';
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    //Get the meta value _magazine-number, for example
    $magazineno = get_post_meta($post->ID, '_magazine-number', true);
    //If there's a value, let's print it.
    if ($magazineno) {
    echo '<li>' . get_the_title() . ' from Magazine #'. $magazineno .'</li>';
    }
    //Otherwise, let's only print the title
    else
    {
    
    		echo '<li>' . get_the_title() . '</li>';
    }
    	}
    	echo '</ul>';
    //Make sure we don't re-use this variable in the next loop
    unset($magazineno);
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    //We're done! Let's return things to normal.
    wp_reset_postdata();

    I wrote all this freehand so please double check it before use. I hope this helps, or at least gets you going in the right direction!

    I missed something. Actually, a few things come to think of it.

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Below is based of their example for “‘orderby’ with ‘meta_value’ and custom post type”

    $args = array(
    	'post_type'  => 'articulos',
    	'meta_key'   => '_article-number',
    	'orderby'    => 'meta_value_num',
    	'order'      => 'ASC',
    	'meta_query' => array(
    		array(
    			'key'     => '_magazine-number',
    			'value'   => '1',
    			'compare' => 'IN',
    		),
    	),
    );
    $query = new WP_Query( $args );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Query with multiple meta values’ is closed to new replies.