WordPress.org

Ready to get started?Download WordPress

Forums

post_category not assigned to posts (6 posts)

  1. Japgalaxy
    Member
    Posted 6 years ago #

    Hi,
    When I save a post the post_category number of the post in the wp_posts table is always setted on "0" zero.
    If I do a query for catch the last post of a category for example:
    (SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_date_gmt < '$today' AND post_category IN (3, 5, 6, 7) ORDER BY post_date DESC LIMIT 0, 5")
    this query don't show, obviously, all the post that have post_category = 0 assigned automatically...
    Is a bug of wordpress? How can I resolve my problem?
    Sorry for my English but I am an italian user...

    Thanks in advance

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    post_category is no longer used. Categories are now just a type of term, and the term_relationships table holds the links between the posts table and the term_taxonomy table (which defines terms as part of a taxonomy.. 'category' in your case).

    If you want all the posts in categories (3,5,6,7), you need to do something more like this:

    SELECT DISTINCT ID, post_title FROM $wpdb->posts as p
    INNER JOIN $wpdb->term_relationships AS tr ON
    (p.ID = tr.object_id AND
    tr.term_taxonomy_id IN (3,5,6,7) )
    INNER JOIN $wpdb->term_taxonomy AS tt ON
    (tr.term_taxonomy_id = tt.term_taxonomy_id AND
    taxonomy = 'category');
  3. Japgalaxy
    Member
    Posted 6 years ago #

    thanks thanks thanks thanks!!! You are my hero!!! :D

  4. IchamArazi
    Member
    Posted 6 years ago #

    Hello Otto. I'm looking for specialist's help. I'm sure you can help me.

    I would like to have something like this :

    <select>
    <option value="{term_taxonomy_id}">Category WP 1</option>
    <option value="{term_taxonomy_id}">Category WP 2</option>
    <option value="{term_taxonomy_id}">Category WP 3</option>
    <option value="{term_taxonomy_id}">...</option>
    </select>

    What is the MySQL Request in order to have all my categories in a select box ?

    Thank you and excuse my poor english (i'm a french guy) !

  5. IchamArazi
    Member
    Posted 6 years ago #

    Hello Otto. I'm looking for specialist's help. I'm sure you can help me.

    I would like to have something like this :

    <select>
    <option value="{term_taxonomy_id}">Category WP 1</option>
    <option value="{term_taxonomy_id}">Category WP 2</option>
    <option value="{term_taxonomy_id}">Category WP 3</option>
    <option value="{term_taxonomy_id}">...</option>
    </select>

    What is the MySQL Request in order to have all my categories in a select box ?

    Thank you and excuse my poor english (i'm a french guy) !

  6. elbuenob
    Member
    Posted 6 years ago #

    Hello! I've got a related question: I'm trying to relate a query to the taxonomy table for each ID that I pull out to see if that post is in a category. Here's my original query:

    $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date), ID
      		FROM $wpdb->posts WHERE MONTH(post_date) = $thismonth
      		AND YEAR(post_date) = $thisyear
      		AND post_status = 'publish' AND post_type='post'
      		AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);
      	if ( $dayswithposts ) {
      		$counter = 0;
    		foreach ( $dayswithposts as $daywith ) {
    
      			$daywithpost[] = $daywith[0];
    
    			//echo $daywithpost[$counter]. " | ";
    			//$counter += 1;
      		}
      	} else {
      		$daywithpost = array();
      	}

    Is there anyway to:
    - Lookup the ID to the Taxonomy table
    - Check for the category name of number
    - Only put those that pass this test into the array

    -Brandon

Topic Closed

This topic has been closed to new replies.

About this Topic